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

What is this not working?

Why is this for loop method not working? When I place this into my own browser it gives me all the number from 4-156 but in this work station it isn't.

script.js
var yes = '';

for (var i = 4; i <= 156; i += 1) {
    yes += i + ' ';

}
console.log(yes);

1 Answer

Hi there,

When I try to run this in the challenge, it gives an error about console.log only being called once - this seems to indicate that the code for checking your answer is specifically looking for you to call a separate console.log for each number - meaning each should be on its own line in the console. With some modifications, we can make this work for your code - basically, you can bypass the 'yes' variable entirely, and just console.log using the i variable inside the loop. It would look something like this:

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

Thank you very much. :-) That makes sense I guess I wasn't understanding the wording of why it was coming up wrong.

No problem - it can definitely be tricky at times!