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

Takuya Hirata
Takuya Hirata
23,296 Points

create a for loop

I am stuck. Please help me out. Thank you very much for your help.

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.

script.js
var number;

for ( var i = 4; i <= 156; i += 1 ) {
      number += i
      console.log(number);
}
Tianni Myers
Tianni Myers
10,453 Points

Try this:

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

3 Answers

Michael Fazekas
Michael Fazekas
630 Points

in your for loop for number += i take out the + sign and make it number = i; don't forget your semicolon.

It looks like there an issue with the variable in the challenge, try logging i to the console.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Looks like your loop is setup correctly, what's the purpose of the variable number? Don't you already have a variable with the value you desire?