Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created March 22, 2018 10:41
Show Gist options
  • Save JackHowa/ef6ed2769f969aaa50dae14d7d26e828 to your computer and use it in GitHub Desktop.
Save JackHowa/ef6ed2769f969aaa50dae14d7d26e828 to your computer and use it in GitHub Desktop.
function checkCashRegister(productPrice, cashGiven, cashInDrawer) {
let changeNeeded = cashGiven - productPrice;
let errors = checkErrors(changeNeeded, cashInDrawer);
if (typeof errors == "string") {
return errors;
} else {
return [];
}
}
function checkErrors(changeNeeded, cashInDrawer) {
let totalCashInDrawer = cashInDrawer.reduce( (total, currencyNameAndCash) => {
return total += currencyNameAndCash[1];
}, 0);
totalCashInDrawer = Math.round(totalCashInDrawer * 100) / 100;
if (totalCashInDrawer < changeNeeded) {
return "Insufficient Funds";
} else if (totalCashInDrawer == changeNeeded) {
return "Closed";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment