Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Refactor Using a Loop

this is RIght ?=

It says I have to make a console.log form 2 to 24 with a loop. i did it? plz help

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

4 Answers

Steven Parker
Steven Parker
210,850 Points

It looks like you're really close, but the challenge asks for just the even numbers. Using "i++" as the increment expression will give you every number in your range.

Also, if you want the end number included, you need a "<=" comparison instead of "<".

It doesn't ask for even numbers... got any other ideas, I changed it to <=.

Steven Parker
Steven Parker
210,850 Points

But it does! The first line of the instructions says: "The code below logs all of the even numbers from 2 to 24 to the JavaScript console.". Your task is to do the same thing in a loop.

This is embarrassing, You had right all the way... sryy.

Raymond Ramos
Raymond Ramos
6,445 Points

if you want it to go all the way to 24, then you have to make i <= 24 the way that you have it right now stops when it checks if 24 < 24 and since it is not it does not print to the console

It's asking you to log only the even numbers, so you need to increment i by 2 instead of one. Also, you need to change your evaluation of "i < 24" to "i < 25" so it will log the final number.

It says: The code below logs all of the even numbers from 2 to 24 to the JavaScript console. However, there's a lot of redundant code here. Re-write this using a loop.

and then there is:

conlsole.log (1); conlsole.log (2); etc.

I dont know What I have to do defferent

Steven Parker
Steven Parker
210,850 Points

Look again, the original code does not log "1", or any odd number.

Charles Buckley
Charles Buckley
21,669 Points

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

Steven Parker
Steven Parker
210,850 Points

:warning: FYI: Explicit answers without any explanation are strongly discouraged by Treehouse and may be subject to redaction by moderators.