> 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](/files/d2SYtc3RgEH2tS2RLeK6)

![recursion definitions](/files/hWJrt8NGfPA1skFNiLyT)

![recursion in a table](/files/tTOJwmIBSDVnOAaeOyzv)
{% 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 %}
