You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
468 B
JavaScript

function amountTocoins(amount, coins)
{
if (amount === 0)
{
return [];
}
else
{
if (amount >= coins[0])
{
left = (amount - coins[0]);
return [coins[0]].concat( amountTocoins(left, coins) );
}
else
{
coins.shift();
return amountTocoins(amount, coins);
}
}
}
console.log(amountTocoins(46, [25, 10, 5, 2,1]));