7

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]
}
7
  • 1
    Have you ever tried [] == [] in the console? Commented Jun 6, 2017 at 4:59
  • 1
    Try 0 == [] and "" == []. Both evaluate to true. Commented Jun 6, 2017 at 4:59
  • put first line of method, input = input || []; and you would save lot of conditions. Commented Jun 6, 2017 at 5:00
  • 3
    Before you consider downvoting, this question does in fact show a proper amount of research and effort. Just because you think the answer is easy doesn't necessarily mean it is. JavaScript equalities can be notoriously tricky, so cut new developers a little slack. Commented Jun 6, 2017 at 5:00
  • It is like comparing two empty boxes. Empty box in your left hand looks just like empty box in your right hand, but they are not the same object. However, they are both empty (length is 0). Commented Jun 6, 2017 at 5:59

3 Answers 3

8

The problem is that arrays are objects. When you compare two objects, you compare their references. Per the MDN documentation:

Equality (==)

If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

Since two instances of arrays don't necessarily have the same reference, the check fails. Just try this in the console:

> [] == []
false

Two arrays seemingly with the same content (or lack thereof) are not equal. That's because content is not checked, but reference. These two arrays are separate instances and refer to different places in memory, thus the check evaluates to false.

On the other hand, just checking the length, and if it is zero checks if the array is empty or not. The length property signifies the amount of content in an array1 and is part of every array. Since it is part of every array and reflects the amount of data in the array, you can use it to check if the array is empty or not.


1 Beware, though, of sparse arrays as mentioned by RobG in the comments. Such arrays can be created with new Array(N) which will give you an empty array, but with length N.

Sign up to request clarification or add additional context in comments.

4 Comments

"[length] …always reflects the amount of data in the array" only when length==0, e.g. new Array(19) creates an empty array with length 19. ;-)
@RobG Yep, I forgot about sparse arrays. I've added a note, thanks
@AndrewLi Great answer. Would the following be an acceptable replacement for OP? ((!input) == [])
@Zze No. When objects are coerced to booleans, they will always be coerced to true regardless of content. It will not give the correct result for arrays that aren't empty.
0

null has no length

  if (input === null || input.length === 0 ) {
    return [];
    }

Comments

0
 int[] res2 = new int[] { };

 if (input == null || input.Length == 0)
       return res2;

This code will solve your problem

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.