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

A question to Dave McFarland / staff regarding this Javascript loop exercise he made

I managed to pass the following challenge:

" 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. "

https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/simplify-repetitive-tasks-with-loops/refactor-using-a-loop

via this code I wrote:

    var x = 0;
        while (x !== 24) {
            x += 2;
            console.log(x);
            }

While this code worked, a bit before I tried the following code that didn't work, and I don't understand why, here is, according to the questions' title, it seems to me it should be work as well:

    var x = 2;
        while (x !== 24) {
            x += 2;
            console.log(x);
            }

I mean, the for loop code that also works in this question:

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

Starts from 2 and works. So why, in while loops, only the code that starts from 0 works?

3 Answers

var x = 0;
  while (x !== 24) {
    x += 2;
    console.log(x);
}

In the code above it would go like this:

  1. Initialize variable x and assign it with the value 0
  2. While checks if the statement is true if yes, loop it! (0 !== 24 is true)
  3. Add 2 to 0 = 2
  4. Log 2
  5. While checks if the statement is true (2 !== 24 still true) etc.

That is why if you want to start with x = 2, you have to put it below the console.log. Otherwise the challenge will fail (log all even numbers from 2-24) as you can see here:

var x = 2;
  while (x !== 24) {
    x += 2;
    console.log(x);
}
  1. Initialize variable x and assign value 2
  2. While checks if the statement is true (2 !== 24 is true)
  3. Add 2 to 2 = 4
  4. Log 4 to the console
  5. Re-entering loop etc.

As you can see here the loop will work, but you will not pass the challenge, because it would log 4 - 24 to the console.

I hope this clears some confusions. Happy coding!

Good to fully understand that when the +=2 is before the console.log(x), the counter must be zero, as increment is an integral part of the iteration, while if it's after console.log(x), it will take var x = 2, but will repeat the process until the number 24 has been reached.

Hi Ben

The challenge asks to log 2-24. The while loop starting with i=2 will increment by 2 first (which equals 4), before logging and spits out numbers from 4-24. If you move the incremental part below the console.log it should work as well. :)

var x = 2;
  while (x !== 24) {
    console.log(x);
    x += 2;
}
Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Ben Aharoni good question. And great answer Egarat Kaoroprat! The real difference is WHEN inside the loop you increment the variable x and WHEN you print to the console. Well explained Egarat! Ben you should select Egarat's answer as the "best answer" so other students know that your question has been answered. Thanks for being a Treehouse student!

Thank you Dave! That's only because I have good teachers here... :)

Thank you for commenting Dave!