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 The Refactor Challenge, Part 2

for, a basic statement

for (var i = 2; i = 24; i +=2) { console.log(i); } This looks like a very basic straightforward "for" loop to me, but my browser wont display it. I can't even open the console to examine it. It has the starting var, the condition and the counter. What am I missing?

6 Answers

Hey John, You are so very close. Based upon your forum post, you have the following code:

for (var i = 2; i = 24; i +=2) { 
    console.log(i); 
}

This does contain expressions for each of the required portions of the for loop as you said. However, notice that the condition portion has a single '=' which then makes it an assignment instead of boolean equality. Moreover, if you did have i == 24 then your loop would not run because 2 == 24 evaluates to false. Try the following and see if you can at least get the loop running:

for(var i = 2; i <=24; i +=2){
     console.log(i); //this should print all of the even numbers from 2 to 24
}

Hope that helps. Happy Coding!

Tyler _
Tyler _
6,651 Points

agreed, how could i == 24 and i == 2 ??

thanks justin, I haven't noticed the term "assignment" used till you just said that. That makes it work. so then does the conditional part part of a "for" loop always have a "<" or ">" in it?

opps, that last "" was supposed to have a greater than, or less than in it. / Does the conditional part of a for loop always have a greater than/less than part?

holy crap, assign a value = assignment...lol. "if I only had a brain" (sings the scare crow from the wizard of oz). MORE COFFEE PLEASE!

Hey John, The conditional part just needs to evaluate to true/false. However, if you are manipulating a range of things then you are going to usually use a >/>=/</<= (relational operators). I would caution to not get stuck in that mindset, because there may be other times that you use other conditions as your logic becomes more complex. At the beginning you will typically see the relational operators.

thanks again justin, I appreciate you bringing in the "big picture" concept. So it's not about "this is how you do it", it's about "what does each situation call for". I like it.

You are welcome. Context is always important. My driving question behind learning and teaching these things is: "Why should I/you care?" Keep on coding. It comes easier and easier.