Diff Two Arrays: Is my solution bad?


Here under my solution that I would like to share. I deleted the numbers found two time during iterating in th array.
is it correct or … not really ?

function diffArray(arr1, arr2) {
  var newArr = [];
  const arrsConcat = arr1.concat(arr2);
  for (let i = 0; i < arrsConcat.length; i++) {
    if (arrsConcat.indexOf(arrsConcat[i], i+1) > 0) {
      arrsConcat.splice(arrsConcat.indexOf(arrsConcat[i], i+1), 1)
      } else {
  return newArr;

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

It passes the challenge so it is correct. I think your algorithm starts off just fine by concatenating the arrays but I think it gets a little messy in the for loop. It obviously works, but I think it can be a little simpler and cleaner. Think about what needs to be done here. You have a concatenated array that includes all of the elements from arr1 and arr2. You want to return an new array that only includes items that are not found in both of the original arrays. In other words:

item is not in arr1 OR item is not in arr2

Does this look like some boolean logic to you? And I bet you can find an easier method for checking if an item is in an array.

Is not the same thing if I say ? :

item is in arr1 OR item is in arr2

I tested the FCC solution without “!” but that doesn’t work and don’t understand really why.

table OR

Both are true…

No, it’s not the same thing. You already know that an item in the concatenated array will always be in one of the two original arrays. So by your logic above the test will always be true because the item will either be in arr1 or in arr2.

What you want is to test if the item is not in one of the arrays. Then you know it’s in one but not the other and you should return it.

Tricky though - you’re using the length of the concatenated array as the exit condition, but then you’re changing the length. Might work here, but this is a troublesome practice to get into.

1 Like

is it the good logic table ? OR exclusive ?

For me, " to test if the item is not in one of the arrays" and to test if the item is in one of the arrays is the same thing with exclusive OR table. :face_with_thermometer:

Yes Snowmonkey, it’s one of the best pratise of the fonctionnal programing. Thanks.

The OR operator is not an “exclusive” OR in JS so you are going down the wrong path here.

ok, so what OR in JS does mean ?

Unless otherwise stated, OR in programming refers to the inclusive OR.

Logical OR (||)

“The logical OR ( || ) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true.”

The difference is that if both sides of the OR are true then the whole thing is still true.

so, we looking for the zeros which return 1, If you are ok, I understand now.
table OR inclusif

In functional programming, you’ll rarely see the use of imperative style. It happens, sure, but it can be avoided.

My point was, you’re indexing your array by an i variable, and you’re looping until that index equals or exceeds the length of the array. With this, in many cases, your i will point to an index just beyond the array eventually, or cause some unintentional strangeness.

Rather than an or logic, i wonder about “not…and” logic: “only include this item if it is not in both array 1 and array 2.”

If it is in one or the other, then it is not in both, returning true.

1 Like

I have an other question about the FCC solution3.

function diffArray(arr1, arr2) {
  return [...diff(arr1, arr2), ...diff(arr2, arr1)];

  function diff(a, b) {
    return a.filter(item => b.indexOf(item) === -1);

In the line two :

  return [...diff(arr1, arr2), ...diff(arr2, arr1)];

“diff” is a function call, isn’t it ?

But at this moment the function is not know since it 's built after the line two. Why it’s not a problem here. I believed that functions must be build before being called, but maybe I’m wrong :thinking:

The function diff is defined right in the diffArray scope - and by using the function diff() method of defining that function, javascript sees that as

var diff = function diff(){...}

And javascript “hoists” var-defined variables to the start of their scope. Doesn’t hoist the assignment, necessarily, except for functions.

Not clear at all, I don’t understand, I think I’m trouble by the association of :

[...         //  this is the "rest"


diff(arr1, arr2),     // this is the function diff()

So “rest” can be used with a function, but I didn’t see that in the lessons. I looked on mdn site and I didn’t see this kind of using.
I will open another topic maybe tomorrow but you can let me a last comment about my last message if you want and I will see it tomorrow.

Good nigth all, thank you again for your time :wink:

Sorry, laziness on my part. That was meant to indicate “the function body here as in the example” without my typing it out.

My point was, when using the function keyword to definea function, javascript places that function in a var (rather than a let or const) type variable. And var type variables actually are declared when the function is initially called, rather then in the line where they first appear.

In this case the ... is actually the spread syntax not the rest parameter.

Spread syntax turns an iterable (such as an array in this case) into a list that can be used to populate an array. Since the diff function returns an array the spread syntax can be used to incorporate the returned array into another array. So:

[...diff(arr1, arr2), ...diff(arr2, arr1)];

is merely “spreading” the arrays returned by the two calls to diff into one array. It is no different than if you did:

[...[0,1,2], ...[3,4,5]]

Which gives you the array [ 0, 1, 2, 3, 4, 5 ]. The solution just happens to be calling the diff function to get the arrays it wants to spread.

1 Like

Helle bbsmooth,

For begining:

Thank you for this comment. I don’t know why I didn’t understand yesterday. I wasn’t well aware of the concatenation. Now it’s clear.

For you last message, I understand all. Just what is trouble again is the fact that “diif” is used in this line:

So before than the function is declared.
First of all, could you confirm me that normally a function must be declared before being used ?
And after explain to me what is special in this case.

This isn’t really true in JavaScript with functions declared with the function keyword in this fashion. So long is the function is declared somewhere in scope, you can call the function (either before or after the definition is written).

If we had instead declared the function with this syntax

  const diff = (a, b) => a.filter(item => b.indexOf(item) === -1);

then the function definition would need to precede any function calls.

1 Like