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 "baboon" then "rumpus"
  when "badger" then "cete"
  when "cobra" then "quiver"
  when "crocodile" then "bask"
"The collective of #{animal} is #{collective}"
# The collective of cobra is quiver

Exercise 2.6.5

animals = 'baboons badgers antelopes cobras crocodiles'

result = for animal in animals.split " "
  collective = switch animal
    when "antelopes" then "herd"
    when "baboons" then "rumpus"
    when "badgers" then "cete"
    when "cobras" then "quiver"
    when "crocodiles" then "bask"
  "A #{collective} of #{animal}"

Exercises 3.1.5

countWords = (text) ->
  words = text.split /[\s,]/
  significantWords = (word for word in words when word.length > 3)
  significantWords.length

everyOtherWord = (text) ->
  words = text.split /[\s,]/
  takeOther = for word, index in words
    if index % 2 then ""
    else word
  takeOther.join(" ").replace /\s\s/gi, " "

Exercises 3.3.4

http = require 'http'
fs = require 'fs'

sourceFile = 'attendees'
fileContents = 'File not read yet.'

readSourceFile = ->
  fs.readFile sourceFile, 'utf-8', (error, data) ->
    if error
      console.log error
    else
      fileContents = data

fs.watchFile sourceFile, readSourceFile

countWords = (text) ->
  text.split(/,/gi).length

readSourceFile sourceFile

server = http.createServer (request, response) ->
  response.end "#{countWords(fileContents)}"

server.listen 8080, '127.0.0.1'

Exercises 3.4.4

accumulate = (initial, items, accumulator) ->
  total = initial
  for item in items
    total = accumulator total, item
  total

sumFractions = (fractions) ->
  accumulator = (lhs, rhs) ->
    if lhs is '0/0'
      rhs
    else if rhs is '0/0'
      lhs
    else
      lhsSplit = lhs.split /\//gi
      rhsSplit = rhs.split /\//gi
      lhsNumer = 1*lhsSplit[0]
      lhsDenom = 1*lhsSplit[1]
      rhsNumer = 1*rhsSplit[0]
      rhsDenom = 1*rhsSplit[1]
      if lhsDenom isnt rhsDenom
        commonDenom = lhsDenom*rhsDenom
      else
        commonDenom = lhsDenom

      sumNumer = lhsNumer*(commonDenom/lhsDenom) + rhsNumer*(commonDenom/rhsDenom)
      "#{sumNumer}/#{commonDenom}"

  accumulate '0/0', fractions, accumulator

console.log sumFractions ['2/6', '1/4']
# '14/24'

Exercises 4.2.3

Exercise 4.6.3

Exercise 4.7.2

views =
  excluded: []
  pages: {}
  clear: ->
    @pages = {}
  increment: (key) ->
   unless key in @excluded
     @pages[key] ?= 0
     @pages[key] = @pages[key] + 1
  ignore: (page) ->
      @excluded = @excluded.concat page
  total: ->
    sum = 0
    for own page, count of @pages
      sum = sum + count
    sum

Exercises 4.8.3

class GranTurismo
  constructor: (options) ->
    @options = options
  summary: ->
    ("#{key}: #{value}" for key, value of @options).join "\n"

options =
  wheels: 'phat'
  dice: 'fluffy'

scruffysGranTurismo = new GranTurismo options

scruffysGranTurismo.summary()
# wheels: phat
# dice: fluffy

Exercise 5.3.3

Exercise 5.8.1

Exercises 7.2.5

Exercise 10.4.4