Created
July 2, 2020 22:01
-
-
Save andreiskandar/e6ce92629d25e02c407b4d694b2f8508 to your computer and use it in GitHub Desktop.
Function to Calculate change and return an object of change denominations
This file contains 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
/* | |
Create a function named calculateChange that takes in a total amount of a bill and the total cash given to pay that bill. | |
Return a new object that describes the total amount of change for the cashier to give back. Omit any types of change | |
that you shouldn't give back, i.e. if you don't give back a twenty dollar bill, don't include it in the results. | |
*/ | |
const currencyDenominations = ['twenty', 'ten', 'five', 'two', 'one', 'quarter', 'dime', 'nickel', 'penny']; | |
const currencyValue = [2000, 1000, 500, 200, 100, 25, 10, 5, 1]; | |
const calculateChange = function(total, cash) { | |
let changeObj = {}; | |
let len = currencyValue.length | |
let change = cash - total; | |
for(let i = 0; i < len; i++){ | |
if(change > currencyValue[i]){ | |
changeObj[currencyDenominations[i]] = Math.floor(change / currencyValue[i]); | |
change = change % currencyValue[i]; | |
} | |
} | |
return changeObj; | |
}; | |
console.log(calculateChange(1787, 2000)); | |
console.log(calculateChange(2623, 4000)); | |
console.log(calculateChange(501, 1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment