BahaaZidan - 1 year ago 68
Javascript Question

# Infinite loop although I put a break;

The aim of this algorithm is to return an organized array (with currencies) of the change .

I built a nested while loop to loop as long as "change" is not equal 0 then each loop in the while loop function while "change" is more than 0 (i.e : not -ve).

What have I missed?

``````//
function checkCashRegister(price, cash, cid) {
var change = cash-price;
var chArr = [["PENNY", 0],
["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0]];

while (change !== 0) {
while (change - 100 > 0) {
chArr[8][1] += 100;
change -=100;
if (change <= 0) {break;}
}
while (change - 20 > 0) {
chArr[7][1] += 20;
change -=20;
if (change <= 0) {break;}
}
while (change - 10 > 0) {
chArr[6][1] += 10;
change -=10;
if (change <= 0) {break;}
}
while (change - 5 > 0) {
chArr[5][1] += 5;
change -=5;
if (change <= 0) {break;}
}
while (change - 1 > 0) {
chArr[4][1] += 1;
change -=1;
if (change <= 0) {break;}
}
while (change - 0.25 > 0) {
chArr[3][1] += 0.25;
change -=0.25;
if (change <= 0) {break;}
}
while (change - 0.1 > 0) {
chArr[2][1] += 0.1;
change -=0.1;
if (change <= 0) {break;}
}
while (change - 0.05 > 0) {
chArr[1][1] += 0.05;
change -=0.05;
if (change <= 0) {break;}
}
while (change - 0.01 > 0) {
chArr[0][1] += 0.01;
change -=0.01;
if (change <= 0) {break;}
}
if (change <= 0) {break;}

}
// Here is your change, ma'am.
return chArr;
}

checkCashRegister(17.46, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
``````

You need just to check the single cash parts and use there only the check if the rest is greater then the change. No need for a breaking condition inside of the while loop.

What is missing, is to check if the change is sufficient.

``````function checkCashRegister(price, cash, cid) {
var change = cash-price;
var chArr = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

while (change >= 100 && cid[8][1] >= 100) {
chArr[8][1] += 100;
cid[8][1] -= 100;
change -=100;
}
while (change >= 20 && cid[7][1] >= 20) {
chArr[7][1] += 20;
cid[7][1] -= 20;
change -=20;
}
while (change >= 10 && cid[6][1] >= 10) {
chArr[6][1] += 10;
cid[6][1] -= 10;
change -=10;
}
while (change >= 5 && cid[5][1] >= 5) {
chArr[5][1] += 5;
cid[5][1] -= 5;
change -=5;
}
while (change >= 1 && cid[4][1] >= 1) {
chArr[4][1] += 1;
cid[4][1] -= 1;
change -=1;
}
while (change >= 0.25 && cid[3][1] >= 0.25) {
chArr[3][1] += 0.25;
cid[3][1] -= 0.25;
change -=0.25;
}
while (change >= 0.1 && cid[2][1] >= 0.1) {
chArr[2][1] += 0.1;
cid[2][1] -= 0.1;
change -=0.1;
}
while (change >= 0.05 && cid[1][1] >= 0.05) {
chArr[1][1] += 0.05;
cid[1][1] -= 0.05;
change -=0.05;
}
while (change > 0 && cid[0][1] >= 0.01) {
chArr[0][1] += 0.01;
cid[0][1] -= 0.01;
change -=0.01;
}

// Here is your change, ma'am.
return chArr;
}

var register1 = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]],
register2 = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]];

console.log(checkCashRegister(17.46, 20.00, register1));
console.log(register1);

console.log(checkCashRegister(19.50, 20.00, register2));
console.log(register2);``````
``.as-console-wrapper { max-height: 100% !important; top: 0; }``

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download