Even though I enter the exact same input as the criteria, the code still doesn't run properly.
--> Link to the CodePen of this error <--
let addOrRem = String(prompt("Do you want to add or remove any content? (add/rem)").trim().toLocaleLowerCase());
if(addOrRem !== "rem" || addOrRem !== "add") {
alert("Invalid answer, try again!");
}
If I write the exact same code in a bundled way, the criteria works properly...
--> Link to the CodePen of this error partially solved <--
let addOrRem = String(prompt("Do you want to add or remove any content? (add/rem)").trim().toLocaleLowerCase());
if(addOrRem !== "rem") {
if(addOrRem !== "add"){
alert("Invalid answer, try again!");
}
}
I was expecting the first code to give the exact same output as the second one. I tried switching it for function "while" but resulted in the same issue.
That's because your condition should actually be an AND, not an OR.
if (addOrRem !== "rem" && addOrRem !== "add")
Consider the fact that addOrRem
can only be equal to one value, so one of addOrRem !== "rem"
and addOrRem !== "add"
will always be true (and thus the entire condition will always be true
with ||
). addOrRem
cannot be simultaneously equal to both "rem"
and "add"
, which is required for your original condition to be false
.