2018-09-30 22:01:58 +00:00
---
id: 59713bd26bdeb8a594fb9413
2020-11-27 18:02:05 +00:00
title: Count the coins
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302238
2021-01-13 02:31:00 +00:00
dashedName: count-the-coins
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
There are four types of common coins in [US ](https://en.wikipedia.org/wiki/United_States ) currency:
2019-02-25 04:36:09 +00:00
< ul >
< li > quarters (25 cents)< / li >
< li > dimes (10 cents)< / li >
< li > nickels (5 cents), and< / li >
< li > pennies (1 cent)< / li >
< / ul >
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
< p > There are six ways to make change for 15 cents:< / p >
2020-11-27 18:02:05 +00:00
2019-02-25 04:36:09 +00:00
< ul >
< li > A dime and a nickel< / li >
< li > A dime and 5 pennies< / li >
< li > 3 nickels< / li >
< li > 2 nickels and 5 pennies< / li >
< li > A nickel and 10 pennies< / li >
< li > 15 pennies< / li >
< / ul >
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
2021-03-09 14:34:10 +00:00
Implement a function to determine how many ways there are to make change for a given input, `cents` , that represents an amount of US pennies using these common coins.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`countCoins` should be a function.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof countCoins === 'function');
2018-09-30 22:01:58 +00:00
```
2021-03-09 14:34:10 +00:00
`countCoins(15)` should return `6` .
2020-11-27 18:02:05 +00:00
```js
2021-03-09 14:34:10 +00:00
assert.equal(countCoins(15), 6);
```
`countCoins(85)` shouls return `163` .
```js
assert.equal(countCoins(85), 163);
```
`countCoins(100)` should return `242` .
```js
assert.equal(countCoins(100), 242);
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-03-09 14:34:10 +00:00
function countCoins(cents) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-03-09 14:34:10 +00:00
function countCoins(cents) {
2018-09-30 22:01:58 +00:00
const operands = [1, 5, 10, 25];
2021-03-09 14:34:10 +00:00
const targetsLength = cents + 1;
2018-09-30 22:01:58 +00:00
const operandsLength = operands.length;
2021-03-09 14:34:10 +00:00
const t = [1];
2018-09-30 22:01:58 +00:00
for (let a = 0; a < operandsLength ; a + + ) {
for (let b = 1; b < targetsLength ; b + + ) {
// initialise undefined target
t[b] = t[b] ? t[b] : 0;
// accumulate target + operand ways
t[b] += (b < operands [ a ] ) ? 0 : t [ b - operands [ a ] ] ;
}
}
return t[targetsLength - 1];
}
```