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 For Loops and Loop Exiting

Henry Garmendia
PLUS
Henry Garmendia
Courses Plus Student 11,762 Points

Is this wrong?

Complete the code below to create a for loop that executes 10 times, writing the value of the variable i to the document each time:

for (var i = 0; i < 11; i ++) { document.write(i); }

and this code will print 10 times, right?

2 Answers

Henry Garmendia
Henry Garmendia
Courses Plus Student 11,762 Points

So, Mr. William you saying;

for (var i = 0; i => 10; i ++) { document.write(i); }

now will print 10 times?

William Li
William Li
Courses Plus Student 26,868 Points

No. remember the loop will STOP executing when the termination condition is false,

for (var i = 0; i >= 10; i ++) { document.write(i); }

initial value of i is 0, this automatically makes its loop termination condition i>=10 false. Thus by writing it this way, it will print ZERO time, as the loop won't be executed at all.

Henry Garmendia
PLUS
Henry Garmendia
Courses Plus Student 11,762 Points

Thank you so much for your time @William, and by the way Happy Thanksgiving!