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 trialHenry Garmendia
Courses Plus Student 11,762 PointsIs 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
William Li
Courses Plus Student 26,868 PointsNo, counting from 0 -> 10, that 11 times.
Henry Garmendia
Courses Plus Student 11,762 PointsThank you so much for your time @William, and by the way Happy Thanksgiving!
Henry Garmendia
Courses Plus Student 11,762 PointsHenry Garmendia
Courses Plus Student 11,762 PointsSo, Mr. William you saying;
for (var i = 0; i => 10; i ++) { document.write(i); }
now will print 10 times?
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsNo. 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.