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

Ethan Tam
Ethan Tam
2,684 Points

What is wrong with my code?

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

script.js
var number;
for (var counter=4; counter<=156; counter+=1) {
  number+=counter
}  
console.log(number)
Crescens Kob
Crescens Kob
19,946 Points

what is the question again?

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Ethan,

There are a few things going wrong here, but you seem to be overthinking and overcomplicating this one a little bit:

  1. You don't need a separate variable declared before the loop, so delete that.
  2. The console.log() needs to be inside of the loop, or it will only execute once... when the loop has finished all iterations.
  3. Because the name variable no longer exists, the incrementing line inside the loop needs to be deleted (and replaced with the console.log().
  4. You need to be logging out the counter, so console.log(name) needs to be console.log(counter).

Give it another go with these in mind. I'm sure you'll get it now. :) If you're still stuck, let me know.

Keep coding! :dizzy:

Ethan Tam
Ethan Tam
2,684 Points

I got it correct. The code was much easier than expected. Thank you so much for the help!