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){
    } */
  return newArr;

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.

Which test case(s) result in null values in the array with your code?

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.

Which specific test case results in null values?

In case you do not know what a test case is, the following is the first:

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); // should return an array.

The second test case:

[“diorite”, “andesite”, “grass”, “dirt”, “pink wool”, “dead shrub”], [“diorite”, “andesite”, “grass”, “dirt”, “dead shrub”] should return [“pink wool”].

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.
[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.

For the test case of diffArray( [1, “calf”, 3, “piglet”], [1, “calf”, 3, 4] ), the correct answer is [“piglet”, 4]. Your code returns the same result. There are no null values in the array returned. In fact, there are no test cases which result in null values in the array returned from your function’s code. If there were, the test would fail.

If your function returned the following for the same test case above, then you could say it returns null values in the array. But it does not do that, so your definition of null values in the array is different than what it means to have null values in the array.

[“piglet”,Null, 4]

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???

You edited your original post with slightly different code. It originally was;

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
	for (var k = 0; k < newArr.length; k++) {
		if (newArr[k] != null) {
	return newArr1;

The code above does not return any null values.

BTW - That little black “fake console” is not the actual browser console. I call it a fake console, because it is an attempt to show results like the real console would show, but it did not know what to do with the empty slots, so it returned null values instead. If you are using Chrome, you can access the browser’s console with Ctrl+Shft+J. If you put console.log(newArr1) for the above code you original posted, it does not have any null values in it.

The edited code with a section commented out, actually would not even run until I commented out one extra closing bracket } near the end of the code. Once I commented it out, the test case of diffArray( [1, “calf”, 3, “piglet”], [1, “calf”, 3, 4] ) still does not result in any null values in the array. However, it does have empty slots which is not the same thing as a null value (see below):

[empty × 3, “piglet”, empty × 3, 4]

I understand why you thought you were seeing null values now, but since I always use the actual browser console, I was not seeing what you were seeing. Combine this with the fact you changed out the code while I was away and you hopefully can understand why i was responding the way I did. I am glad you questioned the results in the fake console, because now you know about the real console to use in the future. 97% of the time, the “fake console” shows the same thing as the browser’s console, but there are some cases where it does not.

Now as far as whether the empty slotted arrays should pass or fail the tests, that is something to consider. Typically you want to avoid writing solutions where you create arrays with empty values. Technically, the tests were confirming you had the correct elements in the array returned. Technically, you did have the values, but you also had some empty slots. Empty slots are not values, so my thinking is the tests are correct. If you think empty slotted arrays should not be allowed, then I suggest reading over How to Report a Bug and provide the code which produces the empty slots and passing tests. A ticket will be created and it will be reviewed by others. Your suggestion may or may not be implemented, but at least you will be able to track the status of the ticket.


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