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

Valerie Smith
PLUS
Valerie Smith
Courses Plus Student 5,244 Points

How do I make the console.log run until the program is over? Challenge is telling me I have it running only once.

Though I entered the code into a workspace and it ran perfectly, the challenge is shooting back that I don't have the console.log running until all the numbers from 4 to 156 are displayed.

Can anyone see something I am missing? Should the console.log be in the braces of the for function?

script.js
var num = '';

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

console.log(num);

1 Answer

Yes, the console.log() method needs to be in the loop body. Btw, you can actually leave out the var num declaration and just use the i variable as the argument in the console.log() method.

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