I'm currently trying to push a variable into an array, and change the value of the variable without changing the value that has been pushed into an array. How do i do this? Here is the section that I'm currently trying to solve.
if (cid[j][1] <= toPay && cid[j][1] !== 0) {
change.push(cid[j]);
toPay = toPay - cid[j][1];
cid[j][1] = 0;
}
Here's the full code:
function checkCashRegister(price, cash, cid) {
let j = 0;
let change = [];
var currency = {
'ONE HUNDRED': 100,
TWENTY: 20,
TEN: 10,
FIVE: 5,
ONE: 1,
QUARTER: 0.25,
DIME: 0.1,
NICKEL: 0.05,
PENNY: 0.01
}
let toPay = cash - price;
let index = 0;
for (let j = cid.length - 1; j >= 0; j--) {
if (toPay == 0) {
break;
}
if (cid[j][1] <= toPay && cid[j][1] !== 0) {
change.push(cid[j]);
toPay = toPay - cid[j][1];
cid[j][1] = 0;
}
if (cid[j][1] > toPay) {
if (currency[cid[j][0]] < toPay) {
index = ~~(toPay / currency[cid[j][0]]);
toPay = (toPay - (index * currency[cid[j][0]])).toFixed(2);
cid[j][1] = (index * currency[cid[j][0]]);
change.push(cid[j]);
}
}
}
return change;
}