JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Jerry Kankelborg
Jerry Kankelborg
1,824 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
176,599 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

Luke Maslany
Luke Maslany
3,534 Points

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. :)