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 trialLindsay Groves
4,811 PointsDo 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
Steve Hunter
57,712 PointsIt 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
Ronav Saundh
8,070 PointsSo 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.
Steve Hunter
57,712 PointsYep - correct.
Steve.
Steve Hunter
57,712 PointsUpvoted.
Starky Paulino
Front End Web Development Techdegree Student 6,398 Pointsvar x = 0; do { console.log('I love JavaScript'); x += 1; } while ( x <= 9 )
Justin Fuller
6,656 PointsIs 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. :(
Steve Hunter
57,712 PointsHi 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.
davidanguiano2
6,859 PointsHa! It's so obvious now. Just have to start counting like a computer programmer.
Steve Hunter
57,712 PointsGlad to help! Keep going and if you have any questions, just post in the Community pages. There is always help available.
Steve.
Lindsay Groves
4,811 PointsLindsay Groves
4,811 PointsThank you, Steve! Totally had it backwards:)
A X
12,842 PointsA X
12,842 PointsArgg, I was thinking about counting in base 10 again vs. remembering that the count starts at 0 and the 10th loop would be 9!