Skip to content

Instantly share code, notes, and snippets.

@prodrammer
Created October 15, 2021 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prodrammer/34111fd5b90b8ca6eacac1aeac13c622 to your computer and use it in GitHub Desktop.
Save prodrammer/34111fd5b90b8ca6eacac1aeac13c622 to your computer and use it in GitHub Desktop.
Coin problem
const calc = (coins, value) => {
return coins.reduce((acc, coin) => {
while (acc.balance >= coin) {
if (!acc.result[coin]) acc.result[coin] = 0;
acc.result[coin]++;
acc.balance -= coin;
}
return acc;
}, { balance: value, result: {}});
}
const coins = [25, 10, 5, 1];
const value = 78;
console.log(calc(coins, value));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment