Diff Two Arrays(Help me out )

Diff Two Arrays(Help me out )
0

#1

Tell us what’s happening:
Guys i can’t figure out what’s wrong with my code. Would really appreciate if someone could help me out …

Your code so far

function diffArray(arr1, arr2) {
  var newArr = [];
  for(var i =0;i<arr1.length;i++){
    
    for(var j=0; j<arr2.length;j++){
      
      if(arr1.indexOf(i)===arr2.indexOf(j))
      {
        arr1.splice(i,1);
        arr2.splice(j,1);
      }
    }
  }
  newArr = arr2.concat(arr1);
  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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:


#2

I added some console.log statements to your code below. Run your script with these to see how things look during code execution.

function diffArray(arr1, arr2) {
  var newArr = [];
  for(var i =0;i<arr1.length;i++){
    
    for(var j=0; j<arr2.length;j++){
      
      console.log('arr1.indexOf(' + i + ') = ' + arr1.indexOf(i) + ' | arr2.indexOf(' + j + ') = ' + arr2.indexOf(j));
      if(arr1.indexOf(i)===arr2.indexOf(j))
      {
        arr1.splice(i,1);
        arr2.splice(j,1);
      }
      console.log('arr1 is:');
      console.log(arr1);
      console.log('arr2 is:');
      console.log(arr2);
      console.log();
    }
  }
  newArr = arr2.concat(arr1);
  return newArr;
}