Why does (0 5 3) return true?
Order of operations causes (0 < 5 < 3)
to be interpreted in javascript as ((0 < 5) < 3)
which produces (true < 3)
and true is counted as 1, causing it to return true.
This is also why (0 < 5 < 1)
returns false, (0 < 5)
returns true, which is interpreted as 1
, resulting in (1 < 1)
.
Why 542 is returning true?
This is because of the Operator precedence
.
Less than (<)
operator is evaluated from left-to-right.
First 5<4
is evaluated to false
then false
is converted to 0
in the next evaluation. Finally 0<2
is evaluated to true
console.log(5<4);// falseconsole.log(0<2);// true
Why does this .every() function return true and then false?
You need to use return
inside .every, currently you are just logging num < 0 which is true for num[0] which is (-1 < 0), but false for num[1] which is (50 >= 0). And since you do not return
is instantly exits the loop as this is considered false
.
let numsA = [-1, 50, 75, 200, 350, 525, 1000];console.log("should return false, because 50 is >= 0");console.log(numsA.every(function(num) { return (num < 0);}));
let numsB = [1, 50, -75, 200, 350, 525, 1000];
console.log(numsB.every(function(num) { return (num < 0);}));
let numsC = [-1, 50, 75, 200, 350, 525, 1000];
console.log(numsC.every(num => num < 0));
let numsD = [-5, 0, 5, 10, 15];console.log("should return true because for each num in numsD, num %5 === 0");console.log(numsD.every(num => { return num % 5 === 0}));//you can omit return with arrow function syntax, as this automatically returns the value within ()console.log(numsD.every(num => (num % 5 === 0)));
Why does my function keep returning True?
You can try any of the following functions:
Function 01:
def in_order(nums):
flag = 0
if all(nums[i] <= nums[i + 1] for i in range(len(nums)-1)):
flag = 1
return flag
Function 02:
def in_order(nums):
i=1
while i< len(nums):
if nums[i] < nums[i-1]:
return False
i=i+1
return True
Driver Code:
if __name__ == '__main__':
# Test out-of-order example
nums1 = [5, 6, 7, 8, 3]
if in_order(nums1):
print('In order')
else:
print('Not in order')
# Test in-order example
nums2 = [5, 6, 7, 8, 10]
if in_order(nums2):
print('In order')
else:
print('Not in order')
Output:
Not in order
In order
Javascript - (10310) return true
10<3<10
is equivalent to (10<3)<10
. 10<3
is false
, false<10
is true.
Why does not my function math.abs return true?
You need to Math.abs the limiet too
Also you had a logical error in your (2) - you did not have more than one minute's difference and you have < not <= so we need a difference
const ms2MMSS = ms => { let minutes = Math.floor(ms / 60000); let seconds = ((ms % 60000) / 1000).toFixed(0); return minutes + ":" + (seconds < 10 ? '0' : '') + seconds; }
function timeout(date1, date2, timeLimit) {
const limiet = Math.abs(timeLimit * 60000);
console.log(ms2MMSS(limiet),ms2MMSS(Math.abs(date1.getTime()-date2.getTime())));
return Math.abs(date1 - date2) < Math.abs(limiet);
}
console.log(timeout(new Date("2019-09-01T08:30:00"), new Date("2019-09-01T11:30:00"), 60), "should return false")
console.log(timeout(new Date("2019-09-01T11:30:00"), new Date("2019-09-01T08:30:00"), 60), "should return false")
console.log(timeout(new Date("2019-09-01T11:30:00"), new Date("2019-09-01T08:30:00"), 190), "should return true")
console.log(1,timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), -5), "should return true")
console.log(2,timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:00:00"), -2), "should return true")
console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 0), "should return false")
console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 1), "should return false")
console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 2), "should return false")
console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 3), "should return true")
Why is 1x3 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 JavaScript why 2 1 0 true while 8 1 1 false?
I'd like to clear this problem using following example.
1 < 2 < 3 // evaluates to true
But
3 > 2 > 1 // evaluates to false
As we know js execute from left to right. Therefor, in first example when statement
1 < 2
is executed it evalutes to true. In programming true refers to 1 and false refers to zero. Then after executing the above statement we have true means 1. Now when this result is combined with next statement
true < 3
Then in short this statement means
1 < 3
because true refers to 1 in programming. Since 1 < 3 is true that's why we have the final result as true.
Coming to the next example 3 > 2 > 1 which evaluates to false because
3 > 2 // evaluates to true
Since above statement is true then in programming sense we got 1(true) and then by combining this result with next statement
true > 1 // evaluates to false
The above statement is in short
1 > 1
Since 1 is not greater than 1 therefor it returns the final result to false.
3 > 2 = true
true > 1 = false
So finally we conclude that by programming sense both statement evaluates to different results but in sense of mathematics both are same. Its a interview question. Hopefully you understand this.
Please let me know if needs any other information. In other case, It might be accepted answer.
Why does 3 2 1 return false while 1 2 3 returns true?
Since 1 < 2
evaluates to true
and 3 > 2
evaluates to true
, you're basically doing :
console.log(true < 3);console.log(true > 1);
Related Topics
How to Implement Routereusestrategy Shoulddetach for Specific Routes in Angular 2
Expression Inside Switch Case Statement
Getattribute() Versus Element Object Properties
How to Maintain a Websockets Connection Between Pages
Check Ctrl/Shift/Alt Keys on 'Click' Event
How to Find a Parent with a Known Class in Jquery
Performance Differences Between Visibility:Hidden and Display:None
How Does Facebook Rewrite the Source Url of a Page in the Browser Address Bar
Multicolor Text Highlighting in a Textarea or Text Input
Cancel a Vanilla Ecmascript 6 Promise Chain
How to Mock an Es6 Module Import Using Jest
Filter Array of Objects with Another Array of Objects
Creating a Jquery Object from a Big HTML-String
Template Does Not Update When Using Ui-Router and Ion-Tabs
Getting Mouse Location in Canvas