Tell us what’s happening:
Hi everyone,
So I don’t know what’s wrong with the i value between the 2 internal whiles inside the outer one. At the point it was supposed to be 7 out of the first internal while and then execute the second internal while and decrease to 6. When it is 6 it should get out of the second internal while and operate the arrays as i=6:
sum+=grade[i];
changeArr[i][1]+=grade[i];
cid[i][1]-=grade[i];
but when it gets inside the second while it just decreases and decreases to i=-1.
I would appreciate a little help here. Thank you.
Your code so far
function checkCashRegister(price, cash, cid) {
let grade=[0.01,0.05,0.1,0.25,1,5,10,20,100,Infinity];
let change=cash-price;
let changeArr=[["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
let sum=0;
while(sum<change) {
let i=0;
while(sum+grade[i+1]<=change) {
i+=1;
}
while(cid[i][1]==0) {
i-=1;
if(i=-1) {
console.log({status: "INSUFFICIENT_FUNDS", change: []},cid);
return {status: "INSUFFICIENT_FUNDS", change: []};
}
}
sum+=grade[i];
changeArr[i][1]+=grade[i];
cid[i][1]-=grade[i];
}
if(cid.every(function(x) {return x[1]==0;})) {
console.log({status: "CLOSED", change: changeArr});
return {status: "CLOSED", change: changeArr};
} else {
console.log({status: "OPEN", change: changeArr.filter(function(x) {return x[1]>0;})});
return {status: "OPEN", change: changeArr.filter(function(x) {return x[1]>0;})};
}
}
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]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0
Challenge Information:
JavaScript Algorithms and Data Structures Projects - Cash Register