February 2015
Intermediate to advanced
124 pages
2h 31m
English
Let’s put these ideas together into an old-school computer program. All we want to do is keep track of where we’re keeping our money across three accounts: checking, savings, and our mattress. We can deposit or withdraw money from each of these accounts. Let’s implement these two actions as methods on an object that represents the account, and return that object from a function so that we can create as many accounts as we need without repeating our implementation code:
| Functions/checkbooks/checkbooks.coffee | |
| | createAccount = (name) -> |
| | { |
| | name: name |
| | balance: 0 |
| | |
| | description: -> |
| | "#{@name}: #{dollarsToString(@balance)}" |
| | |
| | deposit: (amount) -> |
| | @balance += amount |
| | @ |
| | |
| | withdraw: (amount) ... |
Read now
Unlock full access