-1

Just got started with Javascript and struggling with some pretty easy code:

"use strict"; 
    function math () {
        if (action == "+") {
            return answer = firstNumber + secondNumber
        }
        else if (action == "-") {
            return answer = firstNumber - secondNumber
        }
        else {
            while(action != "+" || action != "-") {
                action = prompt('Write an action (only "+" and "-" supported!!!):');
            }
        }
    }
    math(firstNumber, secondNumber,action);
    alert(answer);

Even after condition in loop is false the loop still executes. Can you please explain what went wrong? Not just solution, please.

shakh46
  • 3
  • 1
  • 2
  • The condition in the while loop is never false, that's the thing. It should be and, not or, to express properly the condition you'd like to express... – Konstantin Strukov May 15 '22 at 08:06
  • 1
    `action` cannot be “+” and “-“ at the same time. One of the two conditions will always be true. Think it through with pen and paper. – deceze May 15 '22 at 08:06
  • 1
    Also the while loop seems completely unnecessary. – Jan May 15 '22 at 08:07
  • there are still more errors, like not having parameters in the function and assignment insed of the function to undeclared? variables. – Nina Scholz May 15 '22 at 08:20

4 Answers4

1

while(action != "+" || action != "-") will always evaluate to True

while(action != "+" && action != "-") is what you are looking for

Jerry Wu
  • 26
  • 2
0

The error here lies in the while loop conditional statement.

while(action != "+" || action != "-")

This statement will continue looping if action does not equal "+", or "-". If action is "+", we continue to loop because the action does not also equal "-". To fix this change your operator to an && instead of an || like this:

while(action != "+" && action != "-")
0

First of all you don't need to write a while block there as when the compiler will check first two if statements that is if and else if (in order) and will not execute any of them this automatically means that action != "+" and also != "-" so you can directly write like this :

"use strict"; 
function math () {
    if (action == "+") {
        return answer = firstNumber + secondNumber
    }
    else if (action == "-") {
        return answer = firstNumber - secondNumber
    }
    else {
        action = prompt('Write an action (only "+" and "-" supported!!!):');
        }
    }
}
math(firstNumber, secondNumber,action);
alert(answer);

Also it is not stopping because it will run until the while condition is False but if the action is != "+" or "-" this means while condition will always be true because wrong action has been taken, so while loop will run infinitely... Try replacing while with "if statement" or the || operator in while loop with && operator

vibhum mohan
  • 25
  • 1
  • 6
  • Thanks for the answer. In your code if I write another wrong action wont the answer be undefined? – shakh46 May 15 '22 at 09:04
  • if you will write anything except "+" and "-" it will first check if and else if statement inside math function and will not find any condition matching so the else part will be executed : 'Write an action (only "+" and "-" supported!!! – vibhum mohan May 16 '22 at 10:50
0

I guess, you just made an error when formulating your condition and most likely only want to continue looping until the entered action is neither of the two values. You therefore must use the and && and not the or || operator. In other words: you want to continue until the value is not a plus sign AND not a minus sign.

doberkofler
  • 9,511
  • 18
  • 74
  • 126