Tell us what’s happening:
I realise it is a lot of code to sift through, but all I am wanting to figure out is why i cannot de-reference my variable cd from the parameter cid?
I have tried multiple ways to copy cid:
let cd = cid.slice(0);
let cd = cid.slice(0).concat([]);
let cd = [].concat(cid);
As well as the method below.
Yet, for whatever reason, my altering of the variable cd affects cid.
Your code so far
function checkCashRegister(price, cash, cid) {
let stat = {status: "OPEN", change: []};
let tot = 0;
for (let coin of cid) {
tot += coin[1];
}
let total = tot.toFixed(2);
let newarr = [];
let cd = [...cid];
console.log("cd: " + cd[7]);
function changer(chan) {
if (chan >= 100 && cd[8][1] >= 100) {
cd[8][1] -= 100;
changer(chan - 100);
} else if (chan >= 20 && cd[7][1] >= 20) {
cd[7][1] -= 20;
changer(chan - 20);
} else if (chan >= 10 && cd[6][1] >= 10) {
cd[6][1] -= 10;
changer(chan - 10);
} else if (chan >= 5 && cd[5][1] >= 5) {
cd[5][1] -= 5;
changer(chan - 5);
} else if (chan >= 1 && cd[4][1] >= 1) {
cd[4][1] -= 1;
changer(chan - 1);
} else if (chan >= 0.25 && cd[3][1] >= 0.25) {
cd[3][1] -= 0.25;
changer(chan - 0.25);
} else if (chan >= 0.1 && cd[2][1] >= 0.1) {
cd[2][1] -= 0.1;
changer(chan - 0.1);
} else if (chan >= 0.05 && cd[1][1] >= 0.05) {
cd[1][1] -= 0.05;
changer(chan - 0.05);
} else if (chan >= 0.01 && cd[0][1] >= 0.01) {
cd[0][1] -= 0.01;
changer(chan - 0.01);
}
console.log("cd: " + cd[7][1]);
console.log("cid: " + cid[7][1]);
newarr.push([cd[8][0],cid[8][1]-cd[8][1]],[cd[7][0],cid[7][1]-cd[7][1]],[cd[6][0],cid[6][1]-cd[6][1]],[cd[5][0],cid[5][1]-cd[5][1]],[cd[4][0],cid[4][1]-cd[4][1]],[cd[3][0],cid[3][1]-cd[3][1]],[cd[2][0],cid[2][1]-cd[2][1]],[cd[1][0],cid[1][1]-cd[1][1]],[cd[0][0],cid[0][1]-cd[0][1]]);
console.log(newarr);
return newarr;
}
let obj;
if (total < cash-price) {
stat[status] = "INSUFFICIENT_FUNDS";
stat[change] = [];
} else if (total == cash-price) {
stat[status] = "CLOSED";
stat[change] = [cid];
} else {
stat[status] = "OPEN";
obj = changer(cash-price);
//stat[change] = obj;
}
//console.log("NEW: " +newarr);
//console.log("OBJ: " + obj);
//console.log("STAT: " + stat[change]);
return stat;
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
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/76.0.3809.132 Safari/537.36
.
Link to the challenge: