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

Judit Antos
Judit Antos
1,993 Points

How do I call the console.log method more than one time?

The console.log method seems to be the issue, but I am not sure what it doesn't work. Can someone please take a look at my code and provide some feedback? Thanks.

script.js
var number = '';

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

3 Answers

Ah, your issue appears to be that console.log is being called outside the block of code that the for loop is repeating.

So if you want console.log to be triggered multiple times you will want to move it between the curly braces that denote the for loop's code block.

for (...) {
  // Here is the code block for the for loop.
  // Code here will be run multiple times depending on the for loop's condition.
}

// Code here is outside of the for loop block and will not be repeated.
Judit Antos
Judit Antos
1,993 Points

Thanks Erik, for your reply. I figured it, so I tried this:

var number = ' ';

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

console.log (number); }

Now the problem is "You need to log out EVERY number from 4 to 156 to the console. Your loop calls the console.log() method 152 times." Isn't the goal to call the method 152 times?

For sure, looks like now all you need to do is log the counter you are using with your for loop. In this case your counter is the variable named i.

Once you do that, you shouldn't need the number variable that you have declared.

Judit Antos
Judit Antos
1,993 Points

Thank you!

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