Fixing the problem

Let's open up animal.coffee in our text editor. First, we should add logic for birds if we're going to have them in our pet shop:

  behaviors: ->
    switch @type
      when "cat" then ["meow", null]
      when "dog" then ["bark", "wag"]
      when "rabbit" then [null, "hop hop"]
      when "bird" then ["chirp", "flap"]
      when "horse", "donkey"
        ["neigh", null]
      else
        [null, null]

Second, this bug occurred because the default return value from this function didn't play well with the caller's expectation that it would always be a string. Let's revamp our code in pet_view.coffee to properly handle the case in which all nulls are returned:

    if options.showBehavior
      [sound, action] = @pet.behaviors()
      [behavior, cssClass] = if sound?
        ["#{sound}!", "sound"]
      else if action? ...

Get CoffeeScript Application Development 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.