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 trialEthan Tam
2,684 PointsWhat is wrong with my code?
var number;
for (var counter=4; counter<=156; counter+=1) {
number+=counter
}
console.log(number)
var number;
for (var counter=4; counter<=156; counter+=1) {
number+=counter
}
console.log(number)
2 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Ethan,
There are a few things going wrong here, but you seem to be overthinking and overcomplicating this one a little bit:
- You don't need a separate variable declared before the loop, so delete that.
- The
console.log()
needs to be inside of the loop, or it will only execute once... when the loop has finished all iterations. - Because the
name
variable no longer exists, the incrementing line inside the loop needs to be deleted (and replaced with theconsole.log()
. - You need to be logging out the counter, so
console.log(name)
needs to beconsole.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!
Ethan Tam
2,684 PointsI got it correct. The code was much easier than expected. Thank you so much for the help!
Crescens Kob
19,946 PointsCrescens Kob
19,946 Pointswhat is the question again?