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

for loop in task works for me but the website says it's wrong

Hi everyone. I am stuck in the for-loop task. It says to print out the numbers 4 to 156 in the console. This is my code and it runs perfectly fine, when I test it:

var number = "";

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

But the Website says it's wrong. I am lost. What is wrong with it? Can anyone help me?

script.js
var number = "";

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

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

It seems that you have some extra codes that don't need to be there. You do not need the number variable, nor the addition number + i in the console.log.

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

This is all the challenge is asking for.

akak
akak
29,445 Points

It's probably because you're printing them as a string not as a numbers. When you declare var number = "" you say that this is a string, so all numbers added there are strings as well. If you go and do var number = 0 and then add to it, the challenge should work.

Thanks guys!! It works now