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

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

Struggling with for loops code challenge

The challenge asks to log out every number from 4 to 156.

Thie works in workspaces, but for some reason, it does not work in the code challenge. I can't see where I am going wrong? Help, please!

Code is below.

Thanks, Jiten.

script.js
var html='';

for ( var i = 4; i <= 156; i +=1 ) {
html += '  ' + i + '  ';
}

   console.log(html);

2 Answers

Your code will display every number from 4 to 156, but as one big string.

The challenge is to log out each number individually. To do this, simply move the console log to within the for loop, as shown below:

var html='';

for ( var i = 4; i <= 156; i +=1 ) {
    console.log(i);
}

No worries, yeah you still got the concept of the loop itself right :D