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 while loops, do...while loops, and Loop Conditions

olu adesina
olu adesina
23,007 Points

do while loop helps

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

i put 10 but it seems i'm wrong a little help please

Michelle Cannito
Michelle Cannito
8,992 Points

We're not supposed to just give the answer, so here are some hints:

  1. The quiz uses <=
  2. The counter is starting at 0
  3. Suppose you wanted to log the message 3 times. Starting with 0, what would the counter be when you wrote the 3rd time? (0, 1, 2)
  4. With <= 3 on a do loop, the message would print out 4 times before stopping, right?

1 Answer

andren
andren
28,558 Points

This is a bit of a tricky question designed to catch you out, the answer is actually one less than you would think. This is due to the fact that x starts at 0, not at 1. If you start counting from 0 and end at 9. then you have counted 10 numbers.

It's worth nothing that this is the case because the less than or equal to (<=) condition is used. If the condition had simply been less than (<) which is pretty common as a starting condition in loops like this, then the loop would stop one number before the one provided. Meaning that a condition of (x < 10) would actually result in 10 runs of the loop.