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

What am I doing wrong? this should count from 4 to 156

What am I missing

script.js
var counter = '';

for (var i = 4; i <= 156; i +=1) {
  counter = i;
}
console.log(counter);
Simon Coates
Simon Coates
28,694 Points

If you run your code using developer tools, it will only output 156. The counter stores the value for i from the last iteration of the loop. You can often test javascript code to see if it does what it's meant to.

2 Answers

Jordan Watson
Jordan Watson
14,738 Points

Right I see what you are trying to do here are trying to update the counter variable with the for loops index more or less.

var counter;

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

Okay this is what Ive got! You created the variable as if it held an empty 'String' When its a integer that is getting placed in it none the less. This should console log 156. Hope that helps. Your code works I don't know why you had trouble with it?

Check the developer tools console for the count result :)

Simon Coates
Simon Coates
28,694 Points

try :

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

console.log needs to be inside loop.