0

I don't really see how this could be wrong code.
In few examples, there is no output at all or it's only "10" in this one.

var num2 = 10;
while (num2 >= 10 && num2 <= 40 && num2%2===0){
    console.log(num2);
    num2++;
}

or like this:

var num2 = 10;
while (num2 >= 10 && num2 <= 40){
    if (num2%2===0){
    console.log(num2);
    num2++;
}}
2
  • What do you expect from this code? Try executing it step by step in a debugger Commented May 26, 2018 at 20:32
  • To print even numbers between 10 and 40 Commented May 26, 2018 at 20:33

1 Answer 1

3

Your first loop stops after the first iteration because 11 is not an even number, so num2%2===0 is false.

Your second loop never stops because it only increments num2 if it's even (from 10 to 11), but 11 is not even and so num2 never changes.

Fix:

var num2 = 10;
while (num2 >= 10 && num2 <= 40) {
    if (num2%2===0) {
        console.log(num2);
    }
    num2++;
}

I.e. always increment num2, but only print the even numbers.

Alternatively:

var num2 = 10;
while (num2 >= 10 && num2 <= 40) {
    console.log(num2);
    num2 += 2;
}

I.e. start at an even number and always increment by 2.

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

1 Comment

God, these devil curly brackets in wrong places :D Thanks

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.