Created
October 15, 2021 05:43
-
-
Save prodrammer/34111fd5b90b8ca6eacac1aeac13c622 to your computer and use it in GitHub Desktop.
Coin problem
This file contains hidden or 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 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