I have a problem that I just can't get my head around. I think that it basically boils down to these two lines of code:
const currentDenomination = CASHVALUES[i][1];
change[change.indexOf(CASHVALUES[i])][1] += currentDenomination;
The value at CASHVALUES[i][1] is doubling each time this is carried out. As far as I can see the data in the change array is all that should be affected. I have tried giving currentDenomination a clone of CASHVALUES in case that was causing the issue, but I got the same result. Please see the full offending snippet below.
I hope I have been clear enough, but if any more information is required, please let me know.
function checkCashRegister(price, cash, cid) {
let cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];
const CASHVALUES = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5], ["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.1], ["NICKEL", 0.05], ["PENNY", 0.01]];
let changeDue = (cash - price);
const TotalChangeDue = changeDue
let change = [];
let status = "OPEN";
cid.reverse();
for(let i=0;i<CASHVALUES.length;i++) {
const currentDenomination = CASHVALUES[i][1];
if(changeDue >= currentDenomination) {
if(cid[i][1] > 0) {
cid[i][1] -= currentDenomination;
cid[i][1] = cid[i][1].toFixed(2);
changeDue -= currentDenomination;
changeDue = changeDue.toFixed(2);
if(!change.includes(CASHVALUES[i])) {
change.push(CASHVALUES[i]);
}
else {
change[change.indexOf(CASHVALUES[i])][1] += currentDenomination;
}
i--;
}
}
}
}