What's problem here? Diff Two Arrays

What's problem here? Diff Two Arrays
0

#1

Tell us what’s happening:

Your code so far

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  for(var i = arr1[0]; i <= arr1.length - 1; i++) {
    
    for(var j = arr2[0]; j <= arr2.length -1;j++ ) {
       arr1[i] == arr2[2] ? "next" : newArr.push(arr1[i]);
    }
      
  }
  return newArr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36.

Link to the challenge:


#2

First of all, your loops are counting wrong. var i = arr1[0] will give i the value of the first element. You want to start at the index of the first element which is 0. The same applies to your second for-loop.

Now, this part arr1[i] == arr2[2] ? "next" : newArr.push(arr1[i]); :

  • arr1[i] == arr2[2] only checks arr1 values against the 3rd element of arr2. It should have been arr2[j] to check against all elements.

  • Secondly, this code only gets the elements of the first array that do not exist in the second array. What about the elements of the second array that do not exist in the first? You forget those.