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? [action, "action"] else ["", ""]
This ensures that adding another unknown category in the future won't cause an error in our application...