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

Oscar Kam
Oscar Kam
4,281 Points

Stuck on code challenge: For loops

So I'm stuck on the for loops code challenge. This is my code so far

var number = " ";

for ( var i = 4; i <= 156; i ++ 1 ) { number += i; }

console.log(number);

Its telling me I have a parse error. I have a feeling I did a really small, stupid thing.

Thanks in advance.

script.js
var number = " ";

for ( var i = 4; i <= 156; i ++ 1 ) {
  number += i;
}

console.log(number);
Jesus Marco del Carmen
Jesus Marco del Carmen
10,393 Points

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

Here you go. You don't necessarily need to create the var number=""; Every time the condition (4 <= i <= 156) it will log that number into the console.

Hope this helped.

1 Answer

Robert Stefanic
Robert Stefanic
35,170 Points

It's having trouble because the variable you globally defined is a string because you put it in quotes, like this:

var number = " ";

It can't add an integer to a string. It's like trying to add "mark" to 5.

So just initially define your variable as either:

var number;

or

var number = 0;

Then it'll be able to add the value of i in your loop to the number variable, because it'll know that it's an integer.