Created
January 4, 2023 10:52
-
-
Save Sashkan/91049f6b04cad479932b1109f915aebc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const possibleCoins = [10, 5, 2]; | |
const amount = process.argv.slice(2); | |
function getChange(given) { | |
if (!given || given === 0) { | |
return []; | |
} | |
let change = []; | |
for (const coin of possibleCoins) { | |
if (coin > given) { | |
continue; | |
} | |
const amountToGive = Math.floor(given / coin); | |
const remainder = given % coin; | |
change = change.concat(Array(amountToGive).fill(coin)); | |
change = change.concat(getChange(remainder, possibleCoins)); | |
break; | |
} | |
return change; | |
} | |
console.log(getChange(amount)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment