Skip to content

Instantly share code, notes, and snippets.

@joeljuca
Created August 30, 2023 22:10
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 joeljuca/88bb07991f11e8f4649f8f13b2aa7023 to your computer and use it in GitHub Desktop.
Save joeljuca/88bb07991f11e8f4649f8f13b2aa7023 to your computer and use it in GitHub Desktop.
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