Mini-Project: Checkbook Balancer

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:

 createAccount = (name) ->
  {
  name: name
  balance: 0
 
  description: ->
 "​​#{​@name​}​​: ​​#{​dollarsToString(@balance)​}​​"
 
  deposit: (amount) ->
  @balance += amount
 @
 
  withdraw: (amount) ...

Get CoffeeScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.