Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Joseph Falconer
PLUS
Joseph Falconer
Courses Plus Student 15,887 Points

Is this a bug in the quiz or am I missing something?

The blank space in this question is where I've put 10 in the parenthesis after while. But it says this is wrong! Have I misunderstood <=? Help me please and thank you.

Finish the code in this loop's condition, so that 'I love JavaScript' is printed to the console 10 times:

var x = 0;
do {
  console.log('I love JavaScript');
  x += 1; 
} while ( x <= 10 )

Hi, the answer will be 9 because in a Do while loop it always adds the console.log first before it runs the loop to add the other 9.

In a while loop it only starts running the console.log after the loop runs.

Hope this helps.

2 Answers

Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey Joseph,

you have to write 9 in this case. You have to note that x starts at 0 in this case and the comparison operator is less than or equals and the loop body will be executed 10 times (from 0 to 9). In your case it would be printed out 11 times (from 0 to 10). I hope it's more clear now, if not feel free to ask further questions! :)

Joseph Falconer
PLUS
Joseph Falconer
Courses Plus Student 15,887 Points

Thank you Tobias and Neill. Got it! Your explanations sorted it for me!