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

Page Petty
Page Petty
813 Points

My console says it's right... so why are you Bumming me out???

Why am I getting X Bummer?

script.js
var even = 2;

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

3 Answers

You are only logging 2 to 22. You need to include 24.

Page Petty
Page Petty
813 Points

well, my console shows 2 - 24.

var even = 2;

while (even < 24 ) { console.log(even); even += 2; } head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 2 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 4 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 6 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 8 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 10 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 12 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 14 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 16 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 18 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 20 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 22 24

A while loop executes while a condition is true (in this case even < 24). With your code once even = 24 the condition is no longer met. You can fix it with a condition of even <=24 or even < 25. Either one will pass the challenge.

If you would like to see an alternative try the first example here

What console are you using?

Page Petty
Page Petty
813 Points

actually, those will print 26.

var even = 2;

while (even <= 24 ) { console.log(even); even += 2; } head_vendor-a10c14c9…ed21c5641a3.js:1380 2 head_vendor-a10c14c9…ed21c5641a3.js:1380 4 head_vendor-a10c14c9…ed21c5641a3.js:1380 6 head_vendor-a10c14c9…ed21c5641a3.js:1380 8 head_vendor-a10c14c9…ed21c5641a3.js:1380 10 head_vendor-a10c14c9…ed21c5641a3.js:1380 12 head_vendor-a10c14c9…ed21c5641a3.js:1380 14 head_vendor-a10c14c9…ed21c5641a3.js:1380 16 head_vendor-a10c14c9…ed21c5641a3.js:1380 18 head_vendor-a10c14c9…ed21c5641a3.js:1380 20 head_vendor-a10c14c9…ed21c5641a3.js:1380 22 head_vendor-a10c14c9…ed21c5641a3.js:1380 24 26  ​------- var even = 2;

while (even < 25 ) { console.log(even); even += 2; } head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 2 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 4 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 6 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 8 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 10 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 12 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 14 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 16 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 18 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 20 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 22 head_vendor-a10c14c9feb9430f7c34eae5a45d4840a07bfc74111f6409c51a3ed21c5641a3.js:1380 24 26

Carl Evison
Carl Evison
2,656 Points

Your last console.log will be 22 after that you have incremented the number by 2 which will mean the variable even is now 24.

Your conditional statement is then saying is 24 less than 24 and because It isn't less, it's the same the loop stops running because it only runs if the condition is true.