Hello campers,please help ,this challenge made me so nervous I could not figure it out .
function checkCashRegister(price, cash, cid) {
let obj1={status: "INSUFFICIENT_FUNDS", change: []};
let obj2= {status: "CLOSED", change: []};
let obj3={status: "OPEN", change: []};
let reversed=cid.reverse();
let arr=[100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let currency;
let change=cash-price;
let arrTwo=[];
let inMyRegister;
let len;
// Here is your change, ma'am.
inMyRegister=reversed.reduce(function(acc,item1){
return acc+item1[1];
},0);
if(change>inMyRegister){
return {status: "INSUFFICIENT_FUNDS", change: []};
}
function recurse(i){
currency=currency-arr[i];
}
for(let i=0;i<reversed.length;i++){
currency=reversed[i][1];
len=Math.ceil(reversed[i][1]/arr[i]);
for(let j=0;j<=len;j++){
if(change - currency>=0){
arrTwo.push(currency);
change=change-currency;
break;
}
else if(currency==0){
break;
}
else {
recurse(i);
}
}
}
console.log(arrTwo);
}
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]]);
I know that I am pushing the currency in arrTwo but I would like just to see the result that I have and then I’ll fix that by returning objects.
if you check arrTwo in the console you’ll see thihs
[0, 60, 20, 15, 2, 0, -1.5265566588595902e-15, -1.429412144204889e-15, -7.528699885739343e-16]
I don’t know how to fix that ,please help