Diff in Two Arrays

Diff in Two Arrays
0

#1

Tell us what’s happening:
Can I solve the problem through this solution? If I can then what changes should I made.

Your code so far

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  var result; 
  var result1;
  for(var i=0;i<arr1.length;i++)
  {
    
    for(var j=0;j<arr2.length;j++)
  {
    
    if(arr1[i]!==arr2[j])
     {
//         var value=arr1[i];
//         console.log(value); 
          result = arr1[i];
          newArr.push(result);
         
     }  
    
    else if(arr2[j]!==arr1[i])
    {
          result1 = arr2[j];
          newArr.push(result1);
    }  
    
    else
      {
        console.log("Both are equal"); 
      }
      
  }  
    
    
  }
  
  
  
   return newArr;
  
}  

diffArray([1, 2, 3, 4, 5],[1, 2, 3, 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/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

Your logic is way off. See below where I added 3 console.log statements, so you can see what the values are for the comparisons and what is getting pushed. Use your browser’s console (Ctrl+Shft+J in Chrome) to see the results.

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  var result;
  var result1;
  for (var i = 0; i < arr1.length; i++) {
    for (var j = 0; j < arr2.length; j++) {
      console.log('arr['+i+'] = ' + arr1[i] + ', arr['+j+'] = ' + arr2[j]);
      if (arr1[i] !== arr2[j]) {
        result = arr1[i];
        newArr.push(result);
        console.log(arr1[i] + ' was not equal to ' +  arr2[j] + ', so pushed ' + result);
      }
      else if (arr2[j] !== arr1[i]) {
        result1 = arr2[j];
        newArr.push(result1);
        console.log(arr2[j] + ' was not equal to ' +  arr[i] + ', so pushed ' + result1);
      }
      else {
        console.log("Both are equal");
      }
      console.log()
    }
  }

  return newArr;
}