Console.log selectively displays

Hello,
I’ve declared a global variable and want to display it. ‘console.log’ command will not display if it is placed after a longer code.
Please help me to understand why.

**var change=(cash-price);
console.log(change); //displays fine

//However
var change=(cash-price);
if (condition){
//code
}
console.log(change) //will not display **


function checkCashRegister(price, cash, cid) {
 var output={status: null,change: []}

 //calculate change
 var change=(cash-price);
 
//convert array into object. Calculate total amount of money in the //register
var register=cid.reduce(
 function(obj,arr){
 obj.sum += arr[1]
 obj[arr[0]]=arr[1];
return obj
},
{sum: 0})

//change surplus
if(register.sum > change){
output['status']='OPEN'

var valueH=0
var h=0
while (change>=100 && register['ONE HUNDRED']>=100){
 h=1
 change -= 100;
 register['ONE HUNDRED'] -= 100
 valueH += 100  
}
if(h===1){
output['change'].push(['ONE HUNDRED',valueH])
}

var valueT=0
var t=0
while (change>=20 && register['TWENTY']>=20){
 t=1;
 change -= 20;
 register['TWENTY'] -= 20
 valueT += 20
}
if(t===1){
output['change'].push(['TWENTY',valueT])
}

var valueTN=0
var tN=0
while (change>=10 && register['TEN']>=10){
 tN=1
 change -= 10;
 register['TEN'] -= 10
 valueTN += 10
}
if(tN===1){
output['change'].push(['TEN',valueTN])
}

var valueF=0
var f=0
while (change>=5 && register['FIVE']>=5){
 f=1
 change -= 5;
 register['FIVE'] -= 5
 valueF += 5
}
if(f===1){
output['change'].push(['FIVE',valueF])
}

var valueO=0
var o=0
while (change>=1 && register['ONE']>=1){
 o=1
 change -= 1;
 register['ONE'] -= 1
 valueO +=1
}
if(o===1){
output['change'].push(['ONE',valueO])
}

var valueQ=0
var q=0
while (change>=0.25 && register['QUARTER']>=0.25){
 q=1
 change -= 0.25;
 register['QUARTER'] -= 0.25
 valueQ += 0.25
}
if(q===1){
output['change'].push(['QUARTER',valueQ])
}

var valueD=0
var d=0
while (change>=0.1 && register['DIME']>=0.1){
 d=1
 change -= 0.1;
 register['DIME'] -= 0.1
 valueD += 0.1
}
if(d===1){
output['change'].push(['DIME',valueD])
}

var valueN=0
var n=0
while (change>=0.05 && register['NICKEL']>=0.05){
 n=1
 change -= 0.05;
 register['NICKEL'] -= 0.05
 valueN += 0.05
}
if(n===1){
output['change'].push(['NICKEL',valueN])
}
var valueP=0
var p=0
while (change>=0.001 && register['PENNY']>=0.01){
 p=1
 change -= 0.01;
 register['PENNY'] -= 0.01
 valueP += 0.01
}
if(p===1){
output['change'].push(['PENNY',valueP])
}

change = Math.round(change * 100) / 100;
return output
}

// not enough cash in the register

console.log(change)
//if(register.sum < change){
//var output={status: null,change: []}

//}


}




checkCashRegister(335.0157, 70.49345
, [["PENNY", 1.01], ["NICKEL", 2], ["DIME", 3], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 500]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

Perhaps one of the while loops is running infinitely so the code never reaches the console.log() statement

have you tried logging ‘test’ after each while loop to see if each while loop resolves

edit: It’s probably what JeremyLT said, didn’t catch that return before the console.log() statement

It is also possible you are returning before hitting that point in your function.

Hi Jeremy,
No, I checked that.

Hi David,
That section of the code works. I tested it, before moving to this.
There is something about ‘console.log’ that I am not getting. If type it after some code, it won’t display for some reason.

I have difficulties reading your code as you are not using any indentation

but here I see a return statement then the console.log statement

Hi,
This return is within ‘if’ conditional. I put condition so that conditional statement is not executed.
Nonetheless, if I want to console.log variable after it, it does not display.

P.S.
Sorry about indentation. I’ll change that.