> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/ex/code-wars/changing-money.md).

# Changing Money

換零錢

{% tabs %}
{% tab title="演算法" %}
![recursion example](https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2FzFQX5N4KO6WJHe5CWcnA%2Fchange%20money%201.png?alt=media\&token=1046d6b7-a4b5-4110-a047-046c7402c137)

![recursion definitions](https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2FLGTfmtdl3vDVUqXkWRpI%2Fchange%20money%202.png?alt=media\&token=2c8ffdca-ad00-4ff3-8f98-a8884c21b3fe)

![recursion in a table](https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2F5xq933rRcykbzhxDssXM%2Fchange%20money%203.png?alt=media\&token=c933bb58-660a-4a42-a699-11613d63e3d1)
{% endtab %}

{% tab title="JS" %}

```javascript
function f(money, coins){
    return (
      // base cases
      (money < 0 || coins.length == 0) ? 0 : 
      money === 0                      ? 1 :
      // recursive case
      f(money-coins[0], coins) + f(money, coins.slice(1))
    )
}

[
  f(4, [1,2]),      // 3
  f(10, [5,3,2]),   // 4
  f(11, [5,7]),     // 0
  f(0, [5,7]),      // 1
  
].forEach(x => log(x));
```

{% endtab %}

{% tab title="📙 文獻" %}
{% embed url="<https://andrew.neitsch.ca/publications/m496pres1.nb.pdf>" %}
{% endtab %}

{% tab title="💾 程式" %}

* codewars ⟩ [Counting Change Combinations](https://www.codewars.com/kata/541af676b589989aed0009e7/javascript)  ⭐️
* codepen ⟩ [Change Money](https://codepen.io/lochiwei/pen/bGrwNad?editors=0012)
  {% endtab %}
  {% endtabs %}
