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

Simon walters
PLUS
Simon walters
Courses Plus Student 8,962 Points

Finish the code in this do/while loop, so that the following lines are output to the console:

Hi guys

Can someone please advise me on how to approach this? I have watched the videos leading up to this question numerous times and I am still struggling to understand, I have made several attempts at it but can't complete it. I have answered 4 of the 5 questions correctly which enables me to move forward with the course, but I would still like to understand.

Thanks

Simon

1

3

5

7

9

11

13

15

var x = 1;

do {

console.log('#' + ? );

x += ? ;

} while ( x <= 15 )

2 Answers

Matthew Long
Matthew Long
28,407 Points

You want to log x to the console and increment x by 2 each time in order to get that 1, 3, 5, 7... pattern.

var x = 1;
do {
  console.log('#' + x);
  x += 2; 
} while ( x <= 15 )
Simon walters
Simon walters
Courses Plus Student 8,962 Points

Of course, now that i see it in front of me it makes perfect sense!

Thanks Matthew :)

Thanks Matthew, this was the best response

I understood the code but I feel lost with the mathematics behind this. When x = 1, 1+2 will be 3. But when x=2, 2+2 will be 4 which is not listed. Can you please help me understand this? Thank you

Romain Gaget
Romain Gaget
24,449 Points

you need to increase x by 2 (1,3,5...) and console log x

var x = 1;

do {

console.log('#' + x );

x += 2 ;

} while ( x <= 15 )