Diff two arrays gives ok with wrong answer

I just coded the second intermediate algorithm with a couple of for loops and if statements.
I didn’t use the hints.
I found out that the answer is accepted when the result still contains null values in the array.

To me, it seems like this is not a correct answer and the system shouldn’t accept it. Am I wrong? Or does this need to change?

Report it as a bug. :flushed::flushed::flushed::flushed::flushed::flushed:

post code,
I ran into some issues with that too, before I go on a long spiel, lets see if its the same
also, blur for people who didn’t solve it yet.

How do I blur the code?

okay, found the blur code.
I put one of the solutions (only need one because the logic and the result is the same) that gives null values.
This result passes the test.

Watch out, spoiler beneath

function diffArray(arr1, arr2) {
  var newArr = [];
  // var newArr1 = [];
  
 
  // Same, same; but different.
  for (var i = 0; i < arr1.length; i++){
    for (var j = 0; j < arr2.length; j++) {
      if (arr1[i] === arr2[j]){
        delete arr1[i];
        delete arr2[j];
      } 
    }
  }
  
  newArr = (arr1.concat(arr2));
// use console.log(newArr) here to see the null values in newArr
// with this result, you can get an okay by the program although the answer is clearly wrong.
// edit: let me change this code to make it totally clear. 
/* for (var k = 0; k < newArr.length; k++){
    if (newArr[k] != null){
      newArr1.push(newArr[k]);
    } */
  }
  return newArr;
}

edit:
If you copy paste above code in “Diff two arrays” code field. You’ll get an ok.
If you check on the left side though, you’ll see the answer contains null values.

I gave the code in a reply above these. I just solved the “Diff Two Arrays”, an intermediate algorithm test.
When the result includes the null values (which is weird on its own because I thought it would be “undefined”) it still gives a green light and gives access to the next algorithm.
The question does say that only the values that are not in both arrays should be in the result array.
Having the null values in the array makes for a wrong answer but the program accepts it.

Whatever test you do where there are equal elements in both arrays.
As you can see in the code, I delete the equal elements so whatever element I delete, I get a null value.

sorry, I thought it would be clear that all the cases have null values as soon as there are equal elements in both arrays. So except for the last two cases in “diff two arrays” are all test cases that give null values.
so
[1, 2, 3, 5], [1, 2, 3, 4, 5]
[“diorite”, “andesite”, “grass”, “dirt”, “pink wool”, “dead shrub”], [“diorite”, “andesite”, “grass”, “dirt”, “dead shrub”]
[“andesite”, “grass”, “dirt”, “pink wool”, “dead shrub”], [“diorite”, “andesite”, “grass”, “dirt”, “dead shrub”]
[“andesite”, “grass”, “dirt”, “dead shrub”], [“andesite”, “grass”, “dirt”, “dead shrub”]
[1, “calf”, 3, “piglet”], [1, “calf”, 3, 4]
all give null values
But the fact that I get null values is not the problem. The fact that I can pass the test while the null values are inside the arrays makes it a problem.

my code does not return that, it returns this:
[nul, null, null,“piglet”,null,null,null,4] => not enough nulls

If it didn’t return a null value, why would I claim it did???

My mistake for not mentioning that I edited the code.
Thanks for the mention of the “fake” console.
The problem with empty elements is the array.length and the potential to get elements out of the array.
If I effectively try to use for instance newArr[2] I will get an undefined message. So I think this is a mistake

Thanks for the help.

I assume the test framework/case is using map or similar to iterate over the array, which will ignore holes. Personally I would say it is an error (the solution described is not really correct and the test should pick that up), but yeah should probs be reported as a bug.

You’re seeing this @lawfets because you’re trying to use delete on an array; JS is doing exactly what you’ve told it to do, which also happens to not be what you want it to do.

The are no cases I can think of where using delete on an array is a good idea; it’s not for use with arrays. It will delete any object property you tell it to. As arrays are just basically an object keyed by index, with a length property (an array [1,2,3] is like {'0': 1, '1': 2, '2': 3, length: 3}), if you delete you’re going to do fun things like destroy the indexing:

> let arr =  [1,2,3,4]
> delete arr[1]

So you’ve just deleted 2 from the array, so you’d expect the array to look like [1,3,4]

> arr
> [1, <1 empty item>, 2, 3]

And the second value you’d expect to be 3

> arr[1]
> undefined

Some array nethods ignore holes, which gives some protection from this, some array methods don’t. Use splice to remove elements from arrays, not delete

there is a way to remove undefined elements, so I don’t see the problem in using “delete” to remove an element in an array. Anyhow, I used this example to show how to pass the test without the right result.
But I do agree that using splice() does give a clean result of the bat. And I would use it before using delete.

Thanks for your information

myArr.filter(item => item) or similar, basically whatever you put as the answer to the FCC challenge falsey bouncer - however if you used filter for that, you could just filter for this anyway, you don’t need a loop, and filter removes nulls anyway.

Plus it’s not that you would use splice because it gives a clean result, it’s that you would use splice because using delete on arrays is incorrect, delete breaks how arrays work: it won’t force a reindex or update the length, the two things that make arrays work as arrays in JS