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

James Wood
James Wood
9,804 Points

JavaScript for loop

Here is my code for the for loop challenge.

The task is to log the numbers 4 - 156 to the console and i cant seem to get it to work!

Thank you.

script.js
var counter
for (var i=4; i<=155; i += 1) {
  console.log( counter );
}

3 Answers

nitin suresh
nitin suresh
5,691 Points

For each iteration of the for loop you need to print the value of i , since it is the variable getting updated in for loop. But instead you are printing the value of counter which has the value undefined.

// with counter variabe
for(var counter=4;counter<=156;counter++) {
    console.log(counter);
}

// with i variable
for(var i=4;i<=156;i++) {
    console.log(i);
}
James Wood
James Wood
9,804 Points

Thank you both very much!