JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Review while loops, do...while loops, and Loop Conditions

MIHA WANG
MIHA WANG
Front End Web Development Techdegree Student 6,900 Points

do..while loop

Hi, i have problem with quiz. The quiz say i need to loop 10 times, but i already input the number '10' into the while parenthesis, still doesn't work, Can someone tell me whats going on with this question?

2 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,519 Points
var x = 0;
do {
  console.log('I love JavaScript');
  x += 1; 
} while ( x <= 9 )

The loop executes before checking the condition. The first log x = 0. Then there are logs for x = 1 through 9 including 9 for a total of 10.

MIHA WANG
MIHA WANG
Front End Web Development Techdegree Student 6,900 Points

Sorry for the late reply, thank you for the reply and thank you so much KRIS! But i still have question if the loop executes before checking the condition, why the example below in the while parenthesis, doesnt appear x<=12, but x<= 15?

var x = 1; do { console.log('#' + x); x += 2; } while ( x <= 15 ) (This question also from the quiz)

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,519 Points

For x <=12 the loop will stop when x = 13. Only the numbers 1,3,5,7,9,11 will have be logged.

For x <=15 the loop will stop when x = 17. At that point 1,3,5,7,9,11,13,15 have been logged.

MIHA WANG
MIHA WANG
Front End Web Development Techdegree Student 6,900 Points

var counter = 0; { console.log(counter); counter += 1; } (counter < 10);

So, i dont know i catch your words. But, according to your example, in this question, only log out 0 1 2 3 4 5 6 7 8 9, right?

at the first the var x = 1 is also count into the list? right?