1

I have a basic do while loop where I am executing a prompt's input value, then running it through the condition. For some odd reason when I use !== along with || in the same condition it does not work. I know that I can add additional parameters for it to compare against using isNaN and other logical operators, but it makes no sense why this does not work and I would like to get this route working.

var number;
do {
    number = parseInt(prompt('Enter 1 or 2'));
}
while ((number !== 1) || (number !== 2));

If I were to run the following code with a single expression to compare against, it work no problem, but in the previous statement it does not like have multiple conditions to compare against.

var number;
do {
    number = parseInt(prompt('Enter 1 or 2'));
}
while (number !== 1);

Thanks!

5
  • What exactly are you checking for? Making sure they entered either a 1 or a 2 but nothing else? (Hunch is you need && not ||.) Commented Jun 1, 2016 at 22:19
  • The number will always not be 1 OR not be 2. You want to say that number is not 1 AND is not 2. Commented Jun 1, 2016 at 22:19
  • !(number===1 || number===2), or (number!==1 && number!==2) Commented Jun 1, 2016 at 22:20
  • What exactly do you mean by "does not work"? Assuming the given answer doesn't solve it for you. Commented Jun 1, 2016 at 22:20
  • @nnnnnn Thanks a lot the && statement worked. Totally makes sense to me now! Commented Jun 1, 2016 at 22:31

3 Answers 3

4

If the number is 1, then the LHS is false but it isn't 2 so the RHS is true and the overall test is true.

If the number is not 1, then the LHS is true and the overall test is true.

You need && there so you are saying If the number is not 1 and it is also not 2.

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

Comments

0

starting with a do guaranteed at least 1 calculation. in your case with 2 or comparison, it should be an && operator.

guaranteed 1 calculation before testing for exit

var number;
do {
    number = parseInt(prompt('Enter 1 or 2'));
}
while ((number !== 1) && (number !== 2));

does not guaranteed

 var number;
    while ((number !== 1) && (number !== 2));
    {
        number = parseInt(prompt('Enter 1 or 2'));
    }

1 Comment

Thanks for the quick input!
0

This is a logic problem and it's bound to give true in any case as it is.

var number;
do {
    number = parseInt(prompt('Enter 1 or 2'));
}
while ((number !== 1) || (number !== 2));

number = undefined;// or any other value !==1 AND !==2, then true
number = 1;// !==2 BUT ==1, then true
number = 2;// !==1 BUT ==2, then true

You need to check if both statements are true at once, and not one or the other.

var number;
do {
    number = parseInt(prompt('Enter 1 or 2'));
}
while ((number !== 1) && (number !== 2));

number = undefined;// or any other value !== 1 AND !== 2, then true
number = 1;// !== 2 BUT == 1, then false
number = 2;// !== 1 BUT == 2, then false

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.