# Exact Change program - I am stuck with infinite loop error

Exact Change program - I am stuck with infinite loop error
0

#1

Tell us what’s happening:
I am getting an error:
Error: Potential infinite loop at line 30. To disable loop protection, write: // noprotect as the first line. Beware that if you do have an infinite loop in your code this will crash your browser.

I am lost here and I can’t see the similarity with the issue Exact Change Help. Please help! This is my first time getting stuck. I hope posting a forum is an appropriate thing to do here. Otherwise, guidance is appreciated!

``````function checkCashRegister(price, cash, cid) {

function getSum(a, b){
if(isNaN(a)){
a = 0;
} else if (isNaN(b)){
b = 0;
}
return a+b;
}

function concatArr(a, b){
return a.concat(b);
}

var curValue = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];
var change = cash - price;
var cidTotal = cid.reduce(concatArr).reduce(getSum);
var changeDetails = [];

if (change > cidTotal) {
return "Insufficient Funds";
} else if (change === cidTotal){
return "Closed";
} else {
change = change * 100;

while(change > 0){
for (var j = curValue.length-1; j > 0; j--){
if (curValue[j] < change){
if (cid[j][1] >= change){
changeDetails.push([cid[j][0], Math.floor(cid[j][1]*change/curValue[j])]);
change = change%curValue[j];
} else {
changeDetails.push([cid[j][0], cid[j][1]]);
change = change - cid[j][1];
}
}
}
}

}

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

// Example cash-in-drawer array:
// [["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]]

checkCashRegister(19.50, 20.00,[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
``````

Your Browser User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36`.

#2

Your infinite loop is caused by the change variable never being less than or equal to zero, so the while loop just keeps going forever. For the test case of:

``````checkCashRegister(19.50, 20.00,[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
``````

your else block of code in your nested if statement below is part of the problem:

``````          } else {
changeDetails.push([cid[j][0], cid[j][1]]);
change = change - cid[j][1];
}
``````

cid[j][1] is 0 for the entire execution of the script.

When I use the following console.log statements with your solution for the above test case:

``````    while(change > 0){
for (var j = curValue.length-1; j > 0; j--){
if (curValue[j] < change){
console.log(curValue[j], change)
console.log('cid[j][1] = ' + cid[j][1])
if (cid[j][1] >= change){
changeDetails.push([cid[j][0], Math.floor(cid[j][1]*change/curValue[j])]);
change = change%curValue[j];
console.log('if change = ' + change)
} else {
changeDetails.push([cid[j][0], cid[j][1]]);
change = change - cid[j][1];
console.log('else change = ' + change)
}
}
}
}
``````

you can see what the values are of various variables.

#3

Thank you so much, randelldawson! I got out of looping problem! I just need to work out the logic more. I am almost there!! By the way, how do we view the output from console.log? I am just writing program into free code camp console. Should I run it on browser instead of freecodecamp console?