+ 2

What is the "(i<25)" doing in this code?

I saw it in a lesson and I'm not understanding what it's doing. Is it telling JavaScript to stop printing once it gets to 25? I don't see the purpose of the while statement in this scenario. I understand that it means I is less than 25, but why does it need to be there. https://code.sololearn.com/W9zXzOxl82l8/?ref=app

21st Feb 2018, 4:01 PM
Babydoll Scripts
Babydoll Scripts - avatar
3 odpowiedzi
+ 2
Hi! What you're dealing with is a do-while loop. It'll "do" what's in the body of the loop, between the curly braces, "while" the condition in parentheses is true. In this case it's document.writing as long as i<25, which, since i starts at 20, should be 5 loops. Try doing the JavaScript tutorial on loops for a more thorough understanding of what's going on here, and good luck with your code!
21st Feb 2018, 4:12 PM
Alex Mozeak
Alex Mozeak - avatar
+ 4
'i' is the loop counter variable. As you assumed, it's what is being used to regulate the loop so it knows when it should stop looping. 'i' starts off at 20 and 'i++' increases it by one each iteration. Once it reaches 25, it stops the loop.
21st Feb 2018, 4:08 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Everytime it has written the value and increased the variable by one, it checks if i is less than 25. If so, it will repeat. If not, it will break out of the while statement and continue.
21st Feb 2018, 4:06 PM
ReimarPB
ReimarPB - avatar