skip to Main Content

I know this is common but I’m confused. Can someone explain what’s the problem with my condition? The result below should reflect the else statement since the value of extension is ‘Sr.’

var extension = "Sr.";
if (extension != "Jr." || extension != "Sr." || extension != "I") {
  console.log("Should be false"); // the result goes here in if statement
} else {
  console.log("Should be True");
}

The result of the code above is false it should be true

2

Answers


  1. Well you should read again about logic table

    (extension !="Jr." || extension !="Sr." || extension !="I") <== that mean if result one of these is true (match as result in above case it will return false if equal to extention value) than the else statement will be execute

    Change OR (||) to AND (&&) so all condition are meet with the case.

    Login or Signup to reply.
  2. you should use "AND"(&&) statement to get your required output.
    This will go into else block if the extension is any of (Jr, Sr, I).

    var extension = "Sr.";
    if (extension != "Jr." && extension != "Sr." && extension != "I") {
      console.log("Should be false"); 
    } else {
      console.log("Should be True");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search