Created
August 30, 2023 22:10
-
-
Save joeljuca/88bb07991f11e8f4649f8f13b2aa7023 to your computer and use it in GitHub Desktop.
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 change = (payment, coins) => { | |
let _change = {}, | |
remaining = payment; | |
for (let i = 0; i < coins.sort((x, y) => (x < y ? 1 : -1)).length; i++) { | |
const coin = coins[i]; | |
console.log({ coin }); | |
_change[`${coin}`] = Math.floor(remaining / coin); | |
remaining = remaining - Math.floor(remaining / coin) * coin; | |
console.log(_change); | |
console.log({ remaining }); | |
if (remaining === 0) break; | |
} | |
return _change; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment