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 trialSam Begdouri
2,328 PointsThis quiz question says that I am wrong. I want to know why.
var x = 0;
do {
console.log('I love JavaScript');
x += 1;
} while ( x <= 10 )
In this question, I am asked to add something in the while condition so that "I love JavaScript is printed 10 times; some of it is filled out for me ...}while(x<= ...?... ).
I said 10 then used 11. But it says I am wrong. I want to know why. Can any one explain?
Thank you.
Sam
4 Answers
Austin Lavier
3,949 PointsPossible conditions that would work would include:
while (x<10) or while(x<=9)
The reason is that the while condition will cause the loop to run to the top as long as the condition is met. Right now, as you have it, it will run until x is 11. A helpful way to see what is happening is to have this same loop run in the workspace to see how many times it is displaying "I love Javascript"
Chyno Deluxe
16,936 PointsThe answer to the question is 9.
When dealing with do...while loops. the tasks in the do will always run at least once. After the do finishes its initial run, the while will check if the condition is true, If true, the do scripts will continue to run until the condition is no longer true.
I hope this helps.
DJ Millward
6,651 PointsOr you could drop the equals sign . So that the argument is ' x < 10 ' will rin ten times also.
Chyno Deluxe
16,936 PointsYes that will also log 10 times. because the number of times the loop runs is 9 more times and 9 is less than 10.
Sam Begdouri
2,328 PointsThank you guys. This has helped and given me true understanding.
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 Points//Fixed Code Presentation