Tell us what’s happening: for some reasons the code isn’t getting inside the for and while loops hence it returns the rest array unmodified (in the case of OPENED). i want to know why for while loops aren’t working like it should be
Your code so far
function checkCashRegister(price, cash, cid) {
let rest = [
[ 'ONE HUNDRED', 0 ],
[ 'TWENTY', 0 ],
[ 'TEN', 0 ],
[ 'FIVE', 0 ],
[ 'ONE', 0 ],
[ 'QUARTER', 0 ],
[ 'DIME', 0 ],
[ 'NICKEL', 0 ],
[ 'PENNY', 0 ]
];
var curr = [
[ 'ONE HUNDRED', 100 ],
[ 'TWENTY', 20 ],
[ 'TEN', 10 ],
[ 'FIVE', 5 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'NICKEL', 0.05 ],
[ 'PENNY', 0.01 ]
];
var back = {status: "", change: []}
let change_total = cash - price;
let cid_total = cid.reduce((total, cur) =>total + Math.round( cur[1]*100)/100,0);
console.log(cid_total);
if(cid_total < change_total){
return( {status: "INSUFFICIENT_FUNDS", change : back.change});
}
if(cid_total === change_total){
return {status: "CLOSED", change: cid}
}
console.log(curr[0][1]);
for(let i = 0; i < curr.length; i++){
while(change_total - curr[i][1] > 0){
rest[i][1] = rest[i][1] + curr[i][1];
change_total = change_total - curr[i][1];
}
}
if(cid_total > change_total){
return {status: "OPEN", change: rest};
}
}
console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
Challenge: Cash Register
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register