--- id: 59713bd26bdeb8a594fb9413 title: Contare le monete challengeType: 5 forumTopicId: 302238 dashedName: count-the-coins --- # --description-- Ci sono quattro tipi di monete comuni nella valuta degli [Stati Uniti d'America](https://it.wikipedia.org/wiki/Stati_Uniti_d%27America):

Ci sono sei modi per ottenere 15 centesimi:

# --instructions-- Implementa una funzione che determina quanti modi diversi ci sono per ottenere un certo input, `cents`, che rappresenta il numero di centesimi, usando queste monete comuni. # --hints-- `countCoins` dovrebbe essere una funzione. ```js assert(typeof countCoins === 'function'); ``` `countCoins(15)` dovrebbe restituire `6`. ```js assert.equal(countCoins(15), 6); ``` `countCoins(85)` dovrebbe restituire `163`. ```js assert.equal(countCoins(85), 163); ``` `countCoins(100)` dovrebbe restituire `242`. ```js assert.equal(countCoins(100), 242); ``` # --seed-- ## --seed-contents-- ```js function countCoins(cents) { return true; } ``` # --solutions-- ```js function countCoins(cents) { const operands = [1, 5, 10, 25]; const targetsLength = cents + 1; const operandsLength = operands.length; const t = [1]; 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]; } ```