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

Lindsay Groves
Lindsay Groves
4,811 Points

Do While Loop Quiz Question

During one of the quizzes for Stage 1: Simplify Repetitive tasks with Loops, it asks for the person to complete a do while loop to make "I love JavaScript" print to the console 10 times. Here's the code:

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

Why wouldn't the answer for that blank be 10?

5 Answers

It starts counting from zero and is testing for when x is less than or equal to something , so reaching 10 would be the eleventh test.

The whole loop must run at least once, so to make it run ten times, halt execution when x equals 9

Lindsay Groves
Lindsay Groves
4,811 Points

Thank you, Steve! Totally had it backwards:)

Argg, I was thinking about counting in base 10 again vs. remembering that the count starts at 0 and the 10th loop would be 9!

So the final line of code will be: } while (x <= 9) This is 9 because counting in computer programming starts from: 0 1 2 3 4 5 6 7 8 9 These are 10 numbers, so 9 will be present in the statement. Thank you.

Yep - correct. :+1:

Steve. :smile:

Upvoted. :wink: :+1:

Justin Fuller
Justin Fuller
6,656 Points

Is there a reason document.write isn't allowed in this?

I used the same loop, except

for(i=2; i<25, i+=2){ document.write(i) }

and it was a no-no. :(

Hi Justin,

If you want to write to the web page itself, then use document.write - here, we were asked to output to the console, which wouldn't be visible on the web page.

Steve.

Ha! It's so obvious now. Just have to start counting like a computer programmer.

Glad to help! Keep going and if you have any questions, just post in the Community pages. There is always help available.

Steve.