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 For Loops

Why doesn't my solution for Create a For Loop work?

From: https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/simplify-repetitive-tasks-with-loops/create-a-for-loop

I've followed the logic outlined in the video preceding this code challenge and came up with this as a solution:

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

Now this produced a fail on the code challenge, but I can't really determine why...the only things I can come up with is i is a reserved word, so I can't use it as a variable name. The other thought I had was that the sequence of the conditions in the parens matters, although this wasn't specified in the video. Either that or I have 'code blindness' and I can't see that I made a simple error. Can someone shed some light on this? Thanks!

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Abby,

Nope, i is not reserved, so no need to worry there. :)

It's just a minor syntax error you have. Where are are incrementing the variable you have i + 1. When written this way, the compiler is seeing a mathematical expression and is expecting it to be assigned to another variable.

To add 1 to the variable (increment) you could do i += 1 or the more common (especially for a for loop) is i++ (make sure there are no spaces between the i and the ++.

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

Hope that helps. Keep Coding! :dizzy:

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Oh, I forgot to mention. The order inside the parenthesis does matter, but you have it correct.

  1. Initial value

  2. Condition to meet

  3. Increment value

Ah! I appreciate you explaining the difference between i + 1 vs. i ++ & i += 1. I also appreciate you explaining about i not being reserved. :) I also didn't know that there was a sequence to the parenthesis, so that's new and very useful information. So in the end, it was code blindness!

This is a minor question, but I also wanted to ask then i += 1 will work as correct syntax i =+ 1 will not...can you expand upon why?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

The += or =+ is one of those "just because." :)

I never really thought about it, but I'd guess that when the language was being built, that was the convention decided. I'm sure there is a logical reason (or maybe not).

When I first started JavaScript, I remembered which way it went by thinking is as "take the variable and add + [another value] to what you are already equal = to.

But as for a reason ... that like the question of why isn't "cat" spelled with a "k"? :smiley:

Sorry I don't have a more 'technical' answer for you. :dizzy: