# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/ex/code-wars/changing-money.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
