Javascript expression bug (0 <= 14 < 10)? - javascript

How can this be true?
0 <= 14 < 10
I need to evaluate that a number is in between 0 and 10.
But this breaks my logic.
Shouldn't that expression be false?

This expression:
0 <= 14 < 10
is the same as
(0 <= 14) < 10
which is
1 < 10
true.
What you can do instead is:
if (0 <= x && x < 10) { ...
Python is the only programming language I can think of right now where the expression x < y < z does what you expect it to do.

It's not a bug. It's the way the expression is parsed.
It goes left to right:
0 <= 14 < 10
true < 10
1 < 10
true
As you can see, 0 <= 14 is true, so that's what it's replaced with. true is equal to 1 in JavaScript, and most other C-derived languages.
What you should use is:
(0 <= 14) && (14 < 10)

0 <= 14 < 10
(0 <= 14) < 10
true < 10
1 < 10
true

This expression is not evaluating the way you expect it to. It's equivalent to
((0 <= 14) < 10)
So the first one evaluates and returns true, so you then have true < 10, and true < 10 evaluates to true in JavaScript

NO. That is true.
0 <= 14 is true (same as int(1)) and 1 < 10 is true
do this instead:
if(variable > 0 && variable < 10) {
//do something
}

Also check out: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence

Related

Why is 1<x<3 always true?

I'm learning TypeScript, and in the getting started page, they talk about how unexpected javascript is.
Source: https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html
if ("" == 0) {
// It is! But why??
}
if (1 < x < 3) {
// True for *any* value of x!
}
But I still don't understand why 1<x<3 is always true? For example if I let x=10, it will not be true by logic, but why they said it always true?
1 < x < 3 actually is doing this:
(1 < x) < 3
Or even more long form:
const tempVarA = 1 < x
const tempVarB = tempVarA < 3
So 1 < x is either true or false. Then the next step is true < 3 or false < 3. Those don't make much sense as comparisons, but let's see what javascript does with that:
console.log(true < 3) // true
console.log(false < 3) // true
Weird, but let's dig deeper:
console.log(true >= 0) // true
console.log(true >= 1) // true
console.log(true >= 2) // false
console.log(false >= 0) // true
console.log(false >= 1) // false
console.log(false >= 2) // false
It seems that true is being treated as 1 and false is being treated as 0. To verify that let's compare with == (instead of ===) so that it coerces the type of the data for us.
console.log(true == 1) // true
console.log(true == 0) // false
console.log(false == 1) // false
console.log(false == 0) // true
So 1 < x < 3 is always true because false becomes 0 or true becomes 1, and both 0 and 1 always less than 3.
Explanation:
in javascript, comparison operators <, <=, >, >=, ==, and != coerce their operands to make them comparable when they are different types. So when comparing a boolean to a number it coverts the boolean to a number, 0 or 1.
This is why you should almost always use === instead of ==, and why this is a type error in typescript:
const a = true < 3
// Operator '<' cannot be applied to types 'boolean' and 'number'.(2365)
Short version
Javascript and typescript lack a chainable comparison operator.
Did you mean to do this?
1 < x && x < 3
in other words: true is 1 and false is 0. Therefore 1 < x < 3 with x = 5 is as if it executes (1 <5) <3 and writes to 1 <5 = 1 (true) and then 1 <3 = 1 (true). If instead x were 0? Ok 1 <0 is false (0) consequently 0 <3 is true (1)

Why do both these two solutions for printing all numbers divisible by 3 AND 5 between 5 and 50 work?

This was my solution to the problem:
var count = 5;
while (count <= 50) {
if ((count % 3 || count % 5) === 0) {
console.log(count);
}
count++;
}
This was my the instructors solution:
var count = 5;
while (count <= 50) {
if (count % 3 === 0 && count % 5 === 0) {
console.log(count);
}
count++;
}
Both of these solutions gave the proper answer, but I question why || worked in my problem, rather than &&, I tried both. && means both need to be true for it to properly run, right? So wouldn't that have been the proper use?
var count = 5;
while(count <= 50) {
console.log(`count = ${count} | (count % 3 || count % 5) = ${(count % 3 || count % 5)}`);
if((count % 3 || count % 5) === 0) {
console.log(count);
}
count++;
}
This happens because in all cases where count % 3 === 0 and count % 5 does not, the number takes precedence in the or (||) statement since it's "truthy".
eg.
count = 6, 0 || 1 // 1, since 0 is "falsy"
count = 8, 2 || 3 // 2
count = 15, 0 || 0 // 0
Therefore, it seems you stumbled on the correct solution.
It is a javascript peculiarity you encounter. If count % 3 is 0 it is considered "falsy" and the other calculation will be used. So if either modulus has a remains, that value is used in the final test against 0.
So while both works, your teachers code is more readable; thus better.
The following code
(count % 3 || count % 5) === 0
evaluates first the % operations and then the or to end with ===. If both modulo operations return 0, the or condition also evaluates as 0, that compared with 0 equals true and print the numbers. Otherwise, if any of the modulo values isn't 0, the or operation will result in these numbers, hence it won't be printed.
This is because Javascript evaluates numeric values as boolean being 0 False, and any other value True.
In your case it's a question of parenthesis
1st case of || with parenthesis like this ((count % 3 || count % 5) === 0) it like you say count has to be % by 3 and 5 so it's like &&
2nd case (count % 3 === 0 && count % 5 === 0) you've changed the parenthesis to regroup all
with && in the middle
so as i said it's question of parenthesis that makes both solution give the same result
Every value except 0 is evaluated as true. If there is a number divisible by 3 AND 5 both, then 0 || 0 is 0, which makes your if condition to run. If there is a number that is only divisible by 3 OR 5, lets say 10, then 10%3 is equal to 10 and 10%5 is equal 0. 10 || 0 will be evaluated as true (non-zero). Since non-zero value is not equal to 0 therefore if condition will not execute.
I am not sure but your instructors expression can be said as created from your expression by using Demorgans law.
The second solution is more useful than the first.
Because always "%" operator is computational, not logical

What's the difference between && and || in JavaScript? [duplicate]

This question already has answers here:
Logical operators in JavaScript — how do you use them?
(2 answers)
Closed 5 years ago.
Here's a perfect illustration I saw.
&&
if (num > 5 && num < 10) {
return "Yes";
}
return "No";
||
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
What's the difference between these two?
They are AND (&&) and OR (||) operators.
In your first example the response will only be "Yes" if num is greater than 5 AND less than 10, so any value for num between 5 and 10 (exclusively) will return "Yes".
In your second example "No" will be returned if num is greater than 10 OR less then 5, so any value between 5 and 10 (inclusively) will return "Yes".
To make the expression
num > 5 && num < 10
negative,
!(num > 5 && num < 10)
you need to apply De Morgan's law
!(num > 5) || !(num < 10)
and reverse the condition inside of the parens
num <= 5 || num >= 10
In your question you have the expression with logical Or ||
num > 10 || num < 5
which is, despite the order, obviously not the same as the above expression. The result of comparing values is not the same.

How come this is true? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why does (0 < 5 <3) return true?
How come this is true?:
console.log(100 < 210 < 200); // outputs true
That's equivalent to:
console.log((100 < 210) < 200);
which is equivalent to:
console.log(true < 200);
And this evaluates to true because when using operators like <, true is treated as if it were a 1
Consequently, the following will evaluate to false:
console.log(true < 0)
100 < 210 < 200 is equivalent to (100 < 210) < 200 which is true < 200 which is 1 < 200 which is true.
That last bit (true becoming 1) may be a bit surprising. It's the result of how JavaScript does relational operations (Section 11.8.5 of the spec), which says amongst other things that if the relation is between a non-number (other than null or undefined) and a number, you convert the non-number to a number, and the conversion of true to a number results in 1 (Section 9.3 of the spec.)

What is the best way to check >= and <= in Javascript?

I have a number.
I then have an if statement that I want to go like this:
if (5 <= variable <= 10)
So if the number is between 5 and 10, the statement should be true.
What's the best way to go about this?
Thanks.
it is
if (5 <= variable && variable <= 10)
if ((variable >= 5) && (variable <= 10)) works.
If you do this frequently, consider defining a bounds function:
function inBounds(value, min, max){
return ((value >= min) && (value <= max));
}
Actually, if ( 5>= variable >= 10 ) means if(false)
if you means variable between 5, 10 it should be 5 <= variable <=10, and in javascript, the best convention is const value at left, which is from c language if (v=1), if (1=v) problem. so the best is:
if (5 <= variable && 10 >= variable) {
It is my understanding that at the first conditional false, it stops the checking and moves to the next statement.
Given that, you should examine the most often false condition and put it first.
if ((variable >= 5) && (variable <= 10))
if it is more likely to be less than 4 or
if ((variable <= 10) && (variable >= 5))
if the failure of 10 or greater is more likely to occur.

Categories

Resources