jnbdz
jnbdz

Reputation: 4383

I am having some issues interpreting inequalities with Javascript Operators

For my little app I need to do some very simple math... But for some reason I am having trouble doing it with JavaScript.

Here is the code:

(elLeftY <= elementLeftY <= elRightY)

If one of the "questions" is false but the other one is true this little code will always output true... What I want is that only when the two "questions" are true then it equals true but if one of the two is false then it equals false.

Thanks allot in advance.

Upvotes: 0

Views: 156

Answers (2)

bfavaretto
bfavaretto

Reputation: 71918

You can't do it like that in javascript. You need this instead:

(elLeftY <= elementLeftY) && (elementLeftY <= elRightY)

Here is how your current code is being evaluated:

(elLeftY <= elementLeftY <= elRightY)
((elLeftY <= elementLeftY) <= elRightY)
(true <= elRightY)
(1 <= elRightY)
true

Upvotes: 3

user1252065
user1252065

Reputation:

You cannot cascade equality checks in Javascript, you have to split them into two expressions.

((elLeftY <= elementLeftY) && (elementLeftY <= elRightY))

Upvotes: 2

Related Questions