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 Refactor Using a Loop

How should define the log the even num from 2 to 24

Am not understanding the quiz

script.js
for (var i = 2; i >= 24; i=+ 2){
  console.log(12);
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

Antonio De Rose
Antonio De Rose
20,884 Points
for (var i = 2; i > 24; i+= 2){
//again the same mistake, when entering the loop, i is equal to 2
//then when you compute the condition for the the first time 2 > 24 which results false
//so it will never go to the statement, think of it 2 > 24 is it true or false
//loops, one of the main facts is, to enter in and run in as many times, condition will have to result in true
//when it becomes false, it will exit the loop. should you use i > 24 or i (fill in the blank) 24
  console.log(I); //here, bear it in mind, javascript is case sensitive
}

//when you correct these 2 errors, you will encounter another logic error
//and it will only print from 2 to 22 and will miss the 24
//so correct it

Thanks for been tolerant with my mistakes. Wonderful explanations I appreciate it

Antonio De Rose
Antonio De Rose
20,884 Points
//good approach, I would say, but there are 2 syntax issue, so along with a logic issue
for (var i = 2; i >= 24; i=+ 2){ // loop will not enter in ever, if you think back, i is 2 at start and the condition
//is like 2 >= 24 which is false, will never take into the loop
//then, this is wrong i=+2, should be like i+=2
  console.log(12); // logic issue, this will only print 12 however, the times the loop goes
}

I corrected it but still says undefined num

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

I think there still something wrong with my code.