Tell us what’s happening:
Your code so far
function checkCashRegister(price, cash, cid) {
var denominations = [
{ name: 'ONE HUNDRED', val: 100.00},
{ name: 'TWENTY', val: 20.00},
{ name: 'TEN', val: 10.00},
{ name: 'FIVE', val: 5.00},
{ name: 'ONE', val: 1.00},
{ name: 'QUARTER', val: 0.25},
{ name: 'DIME', val: 0.10},
{ name: 'NICKEL', val: 0.05},
{ name: 'PENNY', val: 0.01}
];
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var totalCid = cid.reduce(function(acc,next){
return acc + next[1];
},0.0);
if (totalCid < change){
return 'Insufficient Funds';
} else if (totalCid === change){
return 'Closed';
}
cid = cid.reverse();
var result = denominations.reduce(function(acc,next,index){
if(change >= next.value){
var currentValue = 0.0;
while(change >= next.value && cid[index][1] >= next.value){
currentValue += next.value;
change -= next.value;
change = Math.round(change * 100) / 100;
cid[index][1] -= next.value;
}
acc.push([next.name,currentValue]);
return acc;
} else {
return acc;
}
},[]);
return result.length > 0 && change === 0 ? result : 'Insufficient Funds';
}
}
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/