# avaScript Algorithms and Data Structures Projects: Cash Register

Good day to all! In my code, when calculating the change, no data is added to the array changeToGive. I can not understand where the error. I would be very grateful for the help.

``````function checkCashRegister(price, cash, cid) {
let changeToGive = [];
let x = 0;
let change = cash - price;
let statusCode = [
{status: "INSUFFICIENT_FUNDS", change: []},
{status: "CLOSED", change: cid},
{status: "OPEN", change: changeToGive}
];
let sumCash = cid.flat(2).filter(function(current){
return typeof current === "number";
}).reduce(function(sum, current){
return sum + current;
}, 0);
sumCash = parseFloat(sumCash.toFixed(2));

if (sumCash < change) {
return statusCode[0];
}

else if (sumCash === change) {
return statusCode[1];
}

else {
function countChange(change, cid, x) {
if (change >= 100) {
while (change >= 100 && cid[8][1] >= 100) {
change -= 100;
cid[8][1] -= 100;
x += 1;
}
changeToGive.push(["ONE HUNDRED", (100 * x)]);
x = 0;
}
if (change >= 20) {
while (change >= 20 && cid[7][1] >= 20) {
change -= 20;
cid[7][1] -= 20;
x += 1;
}
changeToGive.push(["TWENTY", (20 * x)]);
x = 0;
}
if (change >= 10) {
while (change >= 10 && cid[6][1] >= 10) {
change -= 10;
cid[6][1] -= 10;
x += 1;
}
changeToGive.push(["TEN", (10 * x)]);
x = 0;
}
if (change >= 5) {
while (change >= 5 && cid[5][1] >= 5) {
change -= 5;
cid[5][1] -= 5;
x += 1;
}
changeToGive.push(["FIVE", (5 * x)]);
x = 0;
}
if (change >= 1) {
while (change >= 1 && cid[4][1] >= 1) {
change -= 1;
cid[4][1] -= 1;
x += 1;
}
changeToGive.push(["ONE", (1 * x)]);
x = 0;
}
if (change >= 0.25) {
while (change >= 0.25 && cid[3][1] >= 0.25) {
change -= 0.25;
cid[3][1] -= 0.25;
x += 1;
}
changeToGive.push(["QUARTER", (0.25 * x)]);
x = 0;
}
if (change >= 0.1) {
while (change >= 0.1 && cid[2][1] >= 0.1) {
change -= 0.1;
cid[2][1] -= 0.1;
x += 1;
}
changeToGive.push(["DIME", (0.1 * x)]);
x = 0;
}
if (change >= 0.05) {
while (change >= 0.05 && cid[1][1] >= 0.05) {
change -= 0.05;
cid[3][1] -= 0.05;
x += 1;
}
changeToGive.push(["NICKEL", (0.05 * x)]);
x = 0;
}
if (change >= 0.01) {
while (change >= 0.01 && cid[0][1] >= 0.01) {
change -= 0.01;
cid[3][1] -= 0.01;
x += 1;
}
changeToGive.push(["PENNY", (0.01 * x)]);
x = 0;
}
if (change > 0 || changeToGive.length < 1) {
return statusCode[0];
}

}

}
console.log(change);
return statusCode[2];
}

// 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]]

console.log(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
``````

I canâ€™t debug this at the moment, but can I suggest a loop instead of 9 if statements that are almost identical?