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

Immanuel Jaeggi
Immanuel Jaeggi
5,164 Points

do..while loop

I needed to print the following to the console

1

3

5

7

9

etc. but couldn't finish it. What am I missing? Here is the code.

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

2 Answers

Immanuel Jaeggi ,

You were pretty close so don't worry, just had few minor mistakes that anyone could of made. The answer to the quiz is follows (I'll detail why it works after).

Answer

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

The question requires you to :

  • Store a counter in a variable named x
  • Log the value of #x e.g. (#1, #3) to the console.
  • Increment x by 2 to only displayed odd numbers.
var x = 1; // counter
do {
  console.log('#' + x); // display the incremental counter
  x += 2; // increment by 2
} while ( x <= 15 )
Immanuel Jaeggi
Immanuel Jaeggi
5,164 Points

Got it... simply jumping 2 numbers to show odd numbers only. A real help thanks!