# Its not an infinte loop but they say so

Tell us what’s happening:
Describe your issue in detail here.

``````  **Your code so far**
``````
``````
function checkCashRegister(price, cash, cid) {
var result = {
status:"OPEN",
change:[]
};

var currency ={
0.01:"PENNY",
0.05:"NICKEL",
0.1:"DIME",
0.25:"QUARTER",
1:"ONE",
5:"FIVE",
10:"TEN",
20:"TWENTY",
100:"ONE HUNDRED"}

var value ={
"PENNY":0.01,
"NICKEL":0.05,
"DIME":0.1,
"QUARTER":0.25,
"ONE":1,
"FIVE":5,
"TEN":10,
"TWENTY":20,
"ONE HUNDRED":100}

var changeNum = cash - price;

var currenList= [100,20,10,5,1,0.25,0.1,0.05,0.01]

var cashReg={};

var changeDic={};

for (var i =0;i<cid.length;i++){

if(cid[i][1]!=0){

cashReg[cid[i][0]]=Math.round(cid[i][1]/(value[cid[i][0]]))

}

}

function update(coin){
cashReg[coin]-=1
if (cashReg[coin]==0){
delete cashReg[coin]
}
}

if(!changeDic.hasOwnProperty(coin)){
changeDic[coin] = value[coin]

}
else{
changeDic[coin]+=value[coin]
}
}

function check(coin){
return cashReg.hasOwnProperty(coin)
}

while (changeNum>0){

if(Object.keys(cashReg).length ==0){
return{status:"INSUFFICENT_FUNDS",change:[]}

}

for(var z=0;z<currenList.length;z++){

if(changeNum>=currenList[z] && check(currency[currenList[z]])){
update(currency[currenList[z]]);
changeNum=(changeNum-currenList[z]).toFixed(2);

break

}
}

}
if(Object.keys(cashReg).length ==0){
return {status:"CLOSED",change:cid}
}

var entries = Object.entries(changeDic)

return {status:"OPEN",change:entries}

}

console.log( checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36`

Challenge: Cash Register

Well, I don’t know the inner workings, but sometimes “infinite loop” is code for “this is taking forever”.

When I put this in the line after the `while`:

``````console.log(changeNum, typeof changeNum)
``````

I see a few things. First I see that that value is getting changed to a string - a bit odd. Not the end of the world, but a little hinky.

Secondly I see what appears to be an infinite loop when I run it with:

``````checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
``````

I get:

0.5 number
0.49 string
0.49 string
0.49 string
// … 1976 lines of the same
0.49 string
0.49 string
0.49 string
Potential infinite loop detected on line 86. Tests may fail if this is not changed.

That smells an awful lot like an infinite loop to me.

1 Like

Yes thank you, fixed that issue as I realised they compute floats a certain way and thought on how to deal with it when there is enough change but not enuf divisions of currency. Anyways, tgank you for the prompt reply

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.