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 Create a for Loop

Dustin McGrew
Dustin McGrew
1,613 Points

My console log looks like this: 12345678910111213141516......156. Why is that not a correct answer?

This is the code I came up with...

var numbers = '';

for(var i = 4; i < 157; i+=1){ numbers += i; }

console.log(numbers);

1 Answer

Jeff Lemay
Jeff Lemay
14,268 Points
  1. Your console.log should be inside the for loop
  2. You want to print 'i' in the console, not 'numbers'
  3. No need to set an empty 'numbers' variable at the top
for ( var i = 4; i <= 156; i += 1 ) { console.log(i); }
Jeff Lemay
Jeff Lemay
14,268 Points

and 4. In the parameters for the for loop, you already tell the loop to add 1 to 'i' every time you go through the loop (i += 1). No need to set that inside the for loop.

Dustin McGrew
Dustin McGrew
1,613 Points

Ok, I swear this is what I had originally but I got an error about calling the console.log function 156 times.