Tell us what’s happening:
I don’t know how to even begin traversing the cid variable to grab the currencies as well as grabbing only the amount needed for that particular currency when attempting to give them the change. How do I know the point at which I cannot give them the exact change although the amount in register exceeds the change due.
Your code so far
function checkCashRegister(price, cash, cid) {
var change = (cash - price).toFixed(2)
// Here is your change, ma'am.\
var totalInRegister =[]
cid.forEach(element => totalInRegister.push(element[1]))
totalInRegister = totalInRegister.reduce((x, y) => x+y).toFixed(2);
if(change === totalInRegister){
return {status: 'CLOSED', change: cid}
} else if (change > totalInRegister){
return {status: 'INSUFFICIENT_FUNDS', change: []}
}
var currencyValue = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.10,
"QUARTER": 0.25,
"ONE": 1.0,
"FIVE": 5.0,
"TEN": 10.0,
"TWENTY": 20.0,
"ONE HUNDRED": 100.0
};
// I want to traverse the cid variable in reverse order and push each value respectively since this is what the requirements ask for
console.log(cid.reverse())
// This will be my array that I display if status is OPEN
var coinsBills = []
// I'm going to need to push values onto coinsBills in their respective amounts(TENS, FIVE, ONE) but never exceeding the amount for each currency in the associated cid variable. Give Customer the most consolidated change at all times
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register