Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,601 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!