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

frits vd Velden
frits vd Velden
1,860 Points

Quiz keeps telling me I'm wrong about logging numbers, but the preview works fine

With this question: Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) method.

I used this script:

``javascript var html = '';

for (var i = 4; i <= 156; i += 1) { html += i + ' '; } console.log(html); `` In my log it shows the numbers from 4 to 156, but I keep getting this message:

Bummer! You need to log out EVERY number from 4 to 156 to the console. Your loop calls the console.log() method 1 times

Anybody can tell me where I'm going wrong?

script.js
var html = '';

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

2 Answers

Michal Janek
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michal Janek
Front End Web Development Techdegree Graduate 30,654 Points

They need you to log out the numbers during the loop as a part of it. What you are doing is concatenating a string and storing it in a variable and after the loop is done then you console.log it out just once. While visually it may seem similar to what you are expected to do, functionally it is very different. So think about it again where is the difference or check the solution bellow.

This is what they are after:

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

or

for (var i = 4; i < 157; i++ ) {
    console.log(i);
}
frits vd Velden
frits vd Velden
1,860 Points

Hi Michal, I tried something very similar (without adding the variable) but it didn't work, that's why I thought; 'hey, this might work' so I ended up with the code above. Thanks for showing the correct solution!

frits vd Velden
frits vd Velden
1,860 Points

Sorry, I wish I could, but I haven't got the code anymore. Didn't save it.