Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created September 17, 2014 04:34
Show Gist options
  • Save dz1984/cf84c8307c6d44ae15ef to your computer and use it in GitHub Desktop.
Save dz1984/cf84c8307c6d44ae15ef to your computer and use it in GitHub Desktop.
Implement the exercise 3: Counting Change on "Functional Programming Principles in Scala" course via CoffeeScript.
countChange = (money, coins) ->
_targetMoney = money
loops = (_currentMoney, _canUsedCoins) ->
return 0 if (_.isEmpty(_canUsedCoins))
h = _.head _canUsedCoins
t = _.tail _canUsedCoins
newMoney = _currentMoney + h
return 0 if newMoney > _targetMoney
return 1 if newMoney == _targetMoney
loops(newMoney, _canUsedCoins) + loops(_currentMoney, t)
loops(0, coins)
console.log countChange(5,[1,2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment