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

Jaime Carreno
Jaime Carreno
4,257 Points

I believe I have completed the challenge, however, I keep getting an error message. What is wrong with my code?

I'm using the same code in the "Launch Code Editor" and viewing it in the console in the browser. I believe that I'm completing the challenge. From what I understand, the challenge is asking to "output" all the numbers from 4 to 156 to the console. However, although the code is still receiving an error message.

What do you see wrong with my code? Or, am I misunderstanding the challenge?

In advance, thank you.

-Jaime,

script.js
var number = '';

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

console.log(number);

1 Answer

You wont need to declare a variable since you're just printing to the console. The console can go inside the loop.

This will solve the problem:

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

However, if you wished to declare a global variable you could also do:

var number = 0;

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