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

Mustafa Malik
PLUS
Mustafa Malik
Courses Plus Student 1,491 Points

Can't get through this task!

have to use a loop to convert this multiple code in to one single running code.

script.js
console.log(2);
console.log(4);
console.log(6);
console.log(8);
console.log(10);
console.log(12);
console.log(14);
console.log(16);
console.log(18);
console.log(20);
console.log(22);
console.log(24);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

6 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

Looks like your over thinking this a bit. The challenge needs you to use a for loop to count up from one number to another. There is really no need for any random generated numbers. See if you can figure it out following the structure of a for loop below (don't include the quotes).

for(var i = "starting number"; i<="max number"; i+="interval"){
 console.log(i); 
}
Julian Gutierrez
Julian Gutierrez
19,201 Points

For this challenge you need to use a for loop.

Mustafa Malik
PLUS
Mustafa Malik
Courses Plus Student 1,491 Points

Yes! i know and i did. Don't know what i'm doing wrong.

Julian Gutierrez
Julian Gutierrez
19,201 Points

Mind posting your code to better help troubleshoot.

Mustafa Malik
PLUS
Mustafa Malik
Courses Plus Student 1,491 Points

var evenNumber = 2 for ( evenNumber >= 24) { Math.floor(Math.random() * 24 ) + 2; console.log(evenNumber); }

This is giving me syntax error. I have also tried to add another variable inside the loop but it still does not work.

Mustafa Malik
PLUS
Mustafa Malik
Courses Plus Student 1,491 Points

Thank you very much Julian. It was a simple task. You were right i was over thinking it. Thanks again.