Please guys, desperately need your help. If think basically this code structure is quite similar to the solution code. I actually understand how this can be solved, but there’s one issue I can just not understand. Have already tried to debug, so I know exactly that the issue I’m referring to is happening in line 23 (quite in the middle) in else statement.
if (value[0] === currency[i][0]) { value[1] = value[1] + currency[i][1]; return value; }
The issue: Even I think I never change currency[i][1], it gets changed in line 23 anyway. I don’t understand why. Due to this issue the rest doesn’t work properly. Can someone help?
**Your code so far**
function checkCashRegister(price, cash, cid) {
let dif = (cash - price)*100, waehrung, objChange = {status: "", change: []};
const currency = [["PENNY", 1], ["NICKEL", 5], ["DIME", 10], ["QUARTER", 25], ["ONE", 100], ["FIVE", 500], ["TEN", 1000], ["TWENTY", 2000], ["ONE HUNDRED", 10000]];
//Check amount of total money in cid
const totalMoney = cid.reduce( (sum, value) => {
return sum + value[1]*100;
}, 0);
//Check two of three possible states depending on totalMoney in cid
if (dif === totalMoney) {objChange.status = "CLOSE"; objChange.change = cid;}
else if (totalMoney < dif) {objChange.status = "INSUFFICIENT_FUNDS"; objChange.change = [];}
//Last possible state: Calculate exact change money
for (let i = cid.length-1; i >= 0; i--) {
while (cid[i][1] > 0 && currency[i][1] <= dif) {
cid[i][1] = (cid[i][1]*100 - currency[i][1])/100; // Eine Währungseinheit abziehen
dif = dif-currency[i][1];
if (objChange.change.find(element => element[0] === currency[i][0]) === undefined) {
objChange.change.push(currency[i]);
}
else {
objChange.change.forEach(value => {
if (value[0] === currency[i][0]) { value[1] = value[1] + currency[i][1]; return value; }
});
}
}
}
if (dif === 0 && dif !== totalMoney) {objChange.status = "OPEN";}
}
checkCashRegister(35, 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/89.0.4389.114 Safari/537.36 Edg/89.0.774.76
.
Challenge: Cash Register
Link to the challenge: