+ 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
3 Answers
+ 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!
+ 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.
+ 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.