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

to log 2-24 even number to a java console.log

This is my code var x =2; do{ x+=2; console.log(x); }

} while (x < 24);

script.js
var x =2;
do{
  x+=2;
  console.log(x);
}


} while (x < 24);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

You are adding 2 to your x variable before you log it to your console. Remember, the do loop executes in order. Here's you're code with an explanation of what it's doing.

var x =2;               // Declare variable x and assign it the value of 2.

do{
  x+=2;                 // Add 2 to the variable (which makes it equal to 4 the first time this loop is run)
  console.log(x); // Log the value of x to the console
}                         // Loop endlessly


} while (x < 24); // This should produce an error, but you'll never get to it because you've closed your loop already.                
                           // Remove the curly brace from the do loop and just use the one before your while.

Does that help? If you need me to rewrite your code or look at your next attempt, I'd be happy to. :)