Reputation: 5897
I have myself a IF state,
tempNum2 = 4, tempNum2 = 3
answer1 = 1, answer2 = 3, answer3 = 2, answer4 = 4
if( (tempNum2== answer2 || tempNum1== answer2) &&
(tempNum2!= answer1 || tempNum1!= answer1) &&
(tempNum2!= answer4 || tempNum1!= answer4) &&
(tempNum2!= answer3 || tempNum1!= answer3) &&
(asnwer5 == 0))
{
console.log("Correct");
}
now looking at the if statement, tempNum2 is equal to answer4, but in my firefox console, it prints out Correct, looking at the if statement, it should fail. Because tempNum2 is 4, and answer4 = 4, so this if statement should fail, what is wrong with this statement? Dont worry about anything else, I'm sure I have this if statement wrong.
Canvas
Upvotes: 0
Views: 153
Reputation: 1203
(tempNum2!= answer4 || tempNum1!= answer4) &&
This evaluates to true still, as presumably tempNum1 is undefined - thus inequal to answer4.
Remember the differences between your logical operators! or will return true if only one it's conditions is satisfied; and in your example it's always going to be satisfied if certain variables aren't defined - with regards to inequality tests anyway.
The overall structure of using if() statements like this leaves a lot to be desired. It affects readability more than anything. I would seriously consider refactoring your code. Maintaining that will not be fun, nor will debugging little errors - as you've just experiences.
Upvotes: 0
Reputation: 1979
tempnum2!= answer4 || tempNum1!=answer4
is alway true, because 1.) tempnum2 is 3 (first line) and 2.) tempNum1 is not defined. Make sure, that your first line is correct ;).
/E: You have serious typos in you code. You should correct them.
(asnwer5 == 0)
. Did you mean (answer5 == 0)
?Upvotes: 1
Reputation: 39698
You have to have one of all 4 or clauses, as you are using an and everywhere. Plus I don't know what answer 5 is, so I can't speak to that, but it should also be true, however it is defined. It is possible that if you change your &&
to ||,
it will work, but I don't know what logic you are trying to use.
(false || false)&&
(true || true)&&
(true || false)&&
(true|| true)
false && true && true && true
I don't know what logic you want exactly, but your code is doing exactly what you told it to. In fact:
(tempNum2== answer2 || tempNum1== answer2)
So, unless tempNum2= tempNum1= answer2
, this logic will always return false.
Upvotes: 2
Reputation: 12154
I guess you are suppose to write tempNum1 = 4 in first line ..
tempNum1 = 4, tempNum2 = 3
so the code:
tempNum1 = 4, tempNum2 = 3
answer1 = 1, answer2 = 3, answer3 = 2, answer4 = 4
if( (tempNum2== answer2 || tempNum1== answer2) &&
(tempNum2!= answer1 || tempNum1!= answer1) &&
(tempNum2!= answer4 || tempNum1!= answer4) &&
(tempNum2!= answer3 || tempNum1!= answer3) &&
(asnwer5 == 0))
{
console.log("Correct");
}
Upvotes: 0