Created
March 22, 2018 10:41
-
-
Save JackHowa/ef6ed2769f969aaa50dae14d7d26e828 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
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