Infinite loop problems! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Infinite loop problems!

I just finished my first script but it has some infinite loop problems. The code is public. Can someone help me because i dont know how to fix this.

3rd Jul 2017, 9:50 AM
Koen Tonkes
Koen Tonkes - avatar
4 Answers
+ 8
indeed! But mistakes help people learn 😁
3rd Jul 2017, 10:09 AM
jay
jay - avatar
+ 7
see your while statement.. It will always return true as one always is alive. try something like var playGame = true; while(playGame) { if(playerHealth == 0 || dragonHealth == 0) { playGame = false; }
3rd Jul 2017, 10:05 AM
jay
jay - avatar
+ 3
https://code.sololearn.com/WR3QSO7sgq25/?ref=app You're make probably mistake in your loop conditions statements: while (dragonHealth > 0 || playerHealth > 0) ... will be true until oce variable still is positive, but I guess you want stop the 'while' loop as soon as one of each becomes zero or negative: while (dragonHealth > 0 && playerHealth > 0)
3rd Jul 2017, 10:06 AM
visph
visph - avatar
+ 3
@jay, using 'or' operator better safe by doing: if (playerHealth <= 0 || dragonHealth <= 0) ;)
3rd Jul 2017, 10:08 AM
visph
visph - avatar