Skip to content

Instantly share code, notes, and snippets.

@emersxw
Created March 2, 2019 01:14
Show Gist options
  • Save emersxw/5085f718ff6a45da989b7bc42622a561 to your computer and use it in GitHub Desktop.
Save emersxw/5085f718ff6a45da989b7bc42622a561 to your computer and use it in GitHub Desktop.
cents into
function change_machine(cents) {
// quarters (25 cents)
// dimes (10 cents)
// nickels (05 cents)
// pennies (01 cent)
// you shall remove the ifs, they are totally unnecessary
let quarters = 0, dimes = 0, nickels = 0, pennies = 0;
if(cents >= 25) {
quarters = parseInt(cents / 25);
cents = cents - (quarters * 25);
}
if(cents >= 10) {
dimes = parseInt(cents / 10);
cents = cents - (dimes * 10);
}
if(cents >= 5) {
nickels = parseInt(cents / 5);
cents = cents - (nickels * 5);
}
if(cents >= 1) {
pennies = parseInt(cents / 1);
cents = cents - (pennies * 1);
}
return {
quarters,
dimes,
nickels,
pennies
};
}
console.log(change_machine(61));
// { quarters: 2, dimes: 1, nickles: 0, pennies: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment