Skip to content

Instantly share code, notes, and snippets.

@Sashkan
Created January 4, 2023 10:52
Show Gist options
  • Save Sashkan/91049f6b04cad479932b1109f915aebc to your computer and use it in GitHub Desktop.
Save Sashkan/91049f6b04cad479932b1109f915aebc to your computer and use it in GitHub Desktop.
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