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

Jane Wheatley
Jane Wheatley
926 Points

do while #1, #3, #5 ...

I don't understand what I am doing wrong with this question:

var x = 1 do { console.log('#' + _) x += _ ; } while (x <= 15)

where the variables print to 1, 3, 5, 7, 9, 11, 13, 15

I'm filling in the blanks with 2, and 1 respectively, but those are apparently not correct.

You want the program to output odd numbers, starting from 1 (the original value of x), and going to 15, so that it resembles this pattern: #1, #3, #5, ... #15. Right off the bat, we can see that it's going up in increments of 2.

In order to do this, you will need the console to output what is currently stored in x. If you do console.log('#' + x) during the first run of the do-while loop, the program will output #1 as a string (remember, x holds the value of 1 currently).

It then goes to the next line, where x will update and increase itself by a new amount; in this case we put 2, so if x was originally 1, then x += 2 results in x assigning itself a new value of 3.

The program then checks for the condition since it wants to stop when x is equal to 15. But since x is equal to 3, and 3 is less than or equal to 15, the condition still holds true, and the program will loop around until x is finally greater than 15.

1 Answer

Steven Parker
Steven Parker
229,744 Points

Perhaps a couple of hints will help:

  • the first blank represent where the number values will be taken from (what variable?)
  • the second blank represents how far apart the values will be