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 for loop problem

I am stuck on a question within the JavaScript loops and arrays course but I cannot get past a particular problem. It asks: "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."

My code to solve the question:

var html = '  ';

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

Console.log(html);

The error stopping me from progressing:

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

I have run this code in other IDE's and the code does work and compiles as required.

Please help.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi! I believe I remember this challenge, although I can't find it right now and no link was provided. I believe the problem here is that you've gone a little above and beyond the call of duty here. And right now you're assigning the console.log as an object to the variable html.

Try removing the variable html and the console.log(html) and just log the value of i directly to the console. In other words, inside your for loop should only be console.log(i). Hope this helps! :sparkles:

Thanks, both these answers helped. The html variable was created to set the log value to 'empty' before the loop runs in the video before the quiz, so I assumed I needed to do the same in the quiz. Amazing how that confused me, however the feedback on error doesn't give a clear indication of what is actually wrong.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I would encourage you to run this little snippet in the console:

var i = 5;
var html;
html = console.log(i);
console.log(html);

Notice that when you do this, it will show the value of i as 5. But the value of html will actually be the Window object. Because you're not assigning i to html or appending it to the string, you're assigning console.log to html.

Keep in mind that console.log is an object in and of itself and can actually be overwritten.

Here's an example of overwriting console.log:

console.log = -2;
console.log("hi");

If you try this, you'll get an error that console.log isn't a function.

I have a feeling that the video you were watching used the html variable to build up a string with something like html += i so that through every iteration the number was appended onto an empty string. At the end you likely did a document.write(html) which is different :smiley: