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 trialJames Wood
9,804 PointsJavaScript 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.
var counter
for (var i=4; i<=155; i += 1) {
console.log( counter );
}
3 Answers
nitin suresh
5,691 PointsFor 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);
}
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,529 Pointsfor (var i=4; i<=156; i += 1) { // 156
console.log(i); // no need for counter because i is also variable
}
James Wood
9,804 PointsThank you both very much!