This is a question from CodeWars that is named "Count of positives/sum of negatives". It says:
If the input array is empty or null, return an empty array
To check if the array is empty, I decided to check if it was an empty array. When I try to do:
if(input == [])
I fail the test, but if I do:
if(input.length == 0)
I pass the test. An empty array should be equal to [] right? Why is there a difference, and what is the difference between these two checks?
My code is as follows:
function countPositivesSumNegatives(input) {
var a = 0;
var b = 0;
if (input == null) {
return []
}
if (input.length == 0) {
return []
}
for (var i = 0; i < input.length; i++) {
if (input[i] > 0) {
a++;
}
if (input[i] < 0) {
b += input[i];
}
}
return [a, b]
}
[] == []in the console?0 == []and"" == []. Both evaluate totrue.lengthis 0).