Infinite loop, why?


#1

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/3?curriculum_id=506324b3a7dffd00020bf661

I cant figure out why this is a infinite loop..


var numbersGame = 40

while(numbersGame <= 40){
    if(numbersGame === 40){  
     numbersGame--
    }else if(numbersGame < 30){     
        numbersGame += 80
    }else{
        console.log(numbersGame)        
    }
}


#2

it should be.lets go through it......

var numbersGame = 40
while(numbersGame <= 40) // checking true

now it'll go

if(numbersGame === 40){ // checking....... condition true
numbersGame-- // here value 39

then again

while(numbersGame <= 40) // checking true numbersGame = 39

if(numbersGame === 40){ checking....... condition false // numbersGame = 39
numbersGame--
}else if(numbersGame < 30){ checking....... condition false // numbersGame = 39
numbersGame += 80
}else{
console.log(numbersGame) //here prints 39 //**numbersGame = 39**
}
}

again

while(numbersGame <= 40) checking true numbersGame = 39

Continue......................................... infinite loop........................


#3

Ahhhh of course!
Been a long week :sweat:

Thanks for clearing things up!


#4

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