Appendix B. Answers to exercises

About the exercises

These exercises are provided for you to practice using CoffeeScript and also to spend some time reflecting on CoffeeScript. These two activities are an essential component of learning a new programming language. Attempting the exercises is more important than looking at the solutions.

Exercise 2.3.3

torch = price: 21
umbrella = {}
combinedCost = (torch.price || 0) + (umbrella.price || 0)
# 21

Exercise 2.4.4

animal = "crocodile"
collective = switch animal
  when "antelope" then "herd"
  when "baboon" then "rumpus"
  when "badger" then "cete"
  when "cobra" then "quiver"
  when "crocodile" then "bask"
# bask

Exercise 2.5.3

animal = "cobra" collective = switch animal when "antelope" then "herd" when ...

Get CoffeeScript in Action 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.