programming-examples/js/Functions/Write a JavaScript function to convert an amount to coins..js
2019-11-15 12:59:38 +01:00

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]));