0
let number = 0;

while (true) {
    if (number%2 === 0) continue;
    console.log(number);
    number ++;
}

I wanted to infinite loop to print out only odd numbers. But this doesn't seem to work. What am I doing wrong?

5
  • 3
    Run your program by-hand using only a pencil and grid-lined paper. You should see why immediately (if that doesn't work then I assume you don't understand how the continue statement works - in which case you should read this page: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 20, 2021 at 5:54
  • 1
    Q1: what is number%2? Q2: what is the condition for number to increase? Answer these questions two and a bug shall reveal itself to you. Commented Feb 20, 2021 at 5:57
  • 1
    while(true) will block the main thread. In Node.js, this may work smoothly, but in browsers, this will mostly freeze the UI. Commented Feb 20, 2021 at 6:06
  • Sebastian Simon, thanks dude but how come the answer at the bottom works and mine doesn't? They both use while (true) Commented Feb 20, 2021 at 6:07
  • 4
    Think about when number++ ever gets called... Commented Feb 20, 2021 at 6:10

5 Answers 5

1

Let's have a brief understanding of the code execution.

When the following code is executed...:

let number = 0;

while (true) {
    if (number%2 === 0) continue;
    console.log(number);
    number ++;
}

...the following happens:

  1. The first line allocates memory for a variable named number and stores a value of 0.
  2. The while loop starts, and it checks whether the condition is true, and yes it is, and always will be, as the condition is simply true.
  3. Then, it checks whether the number mod 2 returns 0, and definitely, it does, since 0÷2 is 0 and the remainder is 0, so the condition inside the if statement is true, and therefore it executes the code correspondingly. When it sees continue, it jumps back to step 2 and checks the while loop condition, then comes back here, then goes back to step 2, then again comes back here and this never ends(ends when you stop the code execution or close your browser obviously).

Simply put, it never goes forward to the console.log and the number++ line. This is why your code isn't working.

The code from others work because the control actually moves forward in the code and never actually becomes stuck in the above loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this code

let number = 0;

while (true) {
    if (number % 2 != 0)
        console.log(number);
    number ++;
}

The code you're using will not work because you defined number=0 and then you're checking the condition if(number%2==0) continue; so this condition will always return true because the number is defined 0 and the number++ will never increase.

1 Comment

Explain what you changed and why that fixes it.
1
let number = 0;

while (true){ 
    if (++number % 2 == 1){
        console.log(number);
    }
}

no need a continue statement, just check the remainder is 1 the print the value in the loop. you can further shorten the if condition as if (++number % 2)

2 Comments

Explain what you changed and why that fixes it.
@NiettheDarkAbsol added a comment
1

You should increment the number when it is even also, to check the next number and so on:

      let number = 0;

      while (true) {
         if (number%2 === 0){
            number++;
            continue;
         }
         console.log(number);
         number ++;
      }

Comments

1

Whatever number is, increment it first. Otherwise, when it is even, it never reaches to number++ any more.

let number = 0;

while (true) {
    number ++;
    if (number%2 === 0) continue;
    console.log(number);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.