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

Jerry Kankelborg
Jerry Kankelborg
4,885 Points

whats the solution ?

heres my attempt

script.js
var num = 0;
for (var i=4; i<=156; i +=1){
  num += 1;
  console.log(num);
}
Steven Parker
Steven Parker
231,269 Points

Luke Maslany β€” why not post an answer instead of a comment? This will allow voting and give Jerry the option of selecting it as "best answer".

1 Answer

The main issue is that you are starting num with a value of 0. The first time you go through the loop it will increment num by 1 and then log it’s new value, 1, to the console.

You can simplify this by foregoing the num variable and logging the for loop index to console instead. For example:

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

Steven Parker: Ah - now I see what you mean. I had added it as a comment as I hadn't scrolled down far enough on my phone to see that Add an Answer was a different section. :)