JS Logical Madness
This code returns min
how is this possible?
if(prices[i] == 1000 && min == 53){
if(prices[i] < min){
return min;
}
return prices[i];
}
+3
Jonathan Woollett-light
source
to share
2 answers
Values ββare strings. When you use them ==
to compare them with numbers, the numbers are first (internally) converted to strings. However, it <
compares the two strings as strings, and therefore the string "1000" is actually smaller than the string "53" because "1" precedes "5" in the character set.
+5
Pointy
source
to share
Funny things happen when you compare strings, not numbers
+3
epascarello
source
to share