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

JavaScript Loops, Arrays and Objects Challenge 1

Hi there,

I'm suppose to create a For Loop to log numbers from 4 to 156, then use the console.log method to print out the numbers. The challenge keeps saying I have a syntax error and won't let me pass.

Can someone point out to me where and what the error is? Thank you!

script.js
var numbersAll = '';
var firstNumber = 4;

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

3 Answers

You are far off, over-thinking this challenge a bit. JavaScript isn't that picky, and is smarter than you think :)

All you need to do is this:

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

Good luck! ~Alex

you need to either change the number to 157 or add an <= in your condition.

This code challenge is a little weird. I know that this will count from 4 to 156, but the challenge is expecting "i < 156", which is 4 to 155.

You're really close on this one. The for Loop is correct, the only thing you need to do is console the variable i through the loop. You don't need to add other variables, since all we want is the value of i.

console.log(i);

I hope this helps.

However, there another error, because she set "var = i" which causes a syntax error

yup, very true.

Hi everyone,

Thank you so much for your input ! Yes I realised i set var = i, which is wrong.

Thank you for pointing that out! Appreciate it very much ! =)

jayshi
jayshi
5,950 Points

Hi Iris,

First, you had some syntax error in the for loop conditions. "for (var = i ; i <= 156 ; i += 1 )" , here i guess you mean "for(var i= firstNumber; i<= 156 ; i+= 1)";

Second, you put "console.log(...)" outside of the for loop, so it will only be executed once.

There are other errors, but those are more like logic error.

Jay

Hi jayshi,

Yes I looked through all the comments and I found out what was wrong. Thank you so much for the helpful input! I appreciate it very much! =)