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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,441 Points

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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,441 Points

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,655 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.