Skip to content

Instantly share code, notes, and snippets.

@IrhaAli
Created October 11, 2022 15:21
Show Gist options
  • Save IrhaAli/f2e1ecd65bf0fb762c9b5c7186a0df12 to your computer and use it in GitHub Desktop.
Save IrhaAli/f2e1ecd65bf0fb762c9b5c7186a0df12 to your computer and use it in GitHub Desktop.
Provides a cashier information on what type of bills and coins to return.
const calculateChange = function(total, cash) {
const returnedChange = {};
let change = cash - total;
let coins = change - (Math.floor(change / 100) * 100);
let bills = change - coins;
//determine which coins to return, if any
if (Math.floor(coins / 25) !== 0){
returnedChange.quarter = Math.floor(coins / 25);
coins = coins - (Math.floor(coins / 25) * 25);
}if (Math.floor(coins / 10) !== 0){
returnedChange.dime = Math.floor(coins / 10);
coins = coins - (Math.floor(coins / 10) * 10);
}if (Math.floor(coins / 5) !== 0){
returnedChange.nickel = Math.floor(coins / 5);
coins = coins - (Math.floor(coins / 5) * 5);
}if (coins !== 0){
returnedChange.penny = coins;
}
//determine which bills to return, if any
if (Math.floor(bills / 2000) !== 0){
returnedChange.twentyDollar = Math.floor(bills / 2000);
bills = bills - (Math.floor(bills / 2000) * 2000);
}if (Math.floor(bills / 1000) !== 0){
returnedChange.tenDollar = Math.floor(bills / 1000);
bills = bills - (Math.floor(bills / 1000) * 1000);
}if (Math.floor(bills / 500) !== 0){
returnedChange.fiveDollar = Math.floor(bills / 500);
bills = bills - (Math.floor(bills / 500) * 500);
}if (Math.floor(bills / 200) !== 0){
returnedChange.twoDollar = Math.floor(bills / 200);
bills = bills - (Math.floor(bills / 200) * 200);
}if (Math.floor(bills / 100) !== 0){
returnedChange.oneDollar = Math.floor(bills / 100);
}
return returnedChange;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment