.filter by .indexOf

This is for the Difference of Two Arrays challenge at freecodecamp

there’s something about the sintax to .indexOf that I’m not getting right… either, I’m filtering the wrong element or by the wrong element. I either get an empty array or an array with nothing filtered out…

function diffArray(arr1, arr2) {
  //create an empty array
  var newArr = [];
  // Same, same; but different.
var r3 = arr1.concat(arr2);
  console.log(newArr);
  newArr = r3.filter(function(element) {
    //return args.indexOf(element) === -1;
    //filters the initial arr of any args that are contained in it
    if (r3.indexOf(element) === -1) {
      newArr.push(r3[element]);
    } //end of if
  }); //end of .filter, an anounoumous function
console.log(r3);


  return newArr;
  //return newArr;
}

You’re trying to use indexOf inside of the filter. The filter is to filter out options of an array, not add them. A filter will only loop over elements in the array. You will never meet your condition of an element not being in the array inside the filter loop.

Given test cases…
a new array would only contain what is in one of them, so duplicates would be eliminated.

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
// should return an array.
diffArray(
  ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"],
  ["diorite", "andesite", "grass", "dirt", "dead shrub"]
);
//  should return ["pink wool"].
diffArray(
  ["andesite", "grass", "dirt", "pink wool", "dead shrub"],
  ["diorite", "andesite", "grass", "dirt", "dead shrub"]
);
//  should return ["diorite", "pink wool"].
diffArray(
  ["andesite", "grass", "dirt", "dead shrub"],
  ["andesite", "grass", "dirt", "dead shrub"]
);
//  should return [].
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
//  should return [4].
diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]);
//  should return ["piglet", 4].
diffArray([], ["snuffleupagus", "cookie monster", "elmo"]);
//  should return ["snuffleupagus", "cookie monster", "elmo"].
diffArray([1, "calf", 3, "piglet"], [7, "filly"]);
//  should return [1, "calf", 3, "piglet", 7, "filly"].

This worked the first time, but I’m trying to rewrite it, because the lesson plan is suppose to cover .filter, .indexOf, .concat, and .slice, but I didn’t see where’d I’d need slice at yet either…

function diffArray(arr1, arr2) {
  //create an empty array
  var newArr = [];
  // Same, same; but different.
  for(var v=0;v<arr1.length;v++){
    if(arr2.indexOf(arr1[v])===-1){
      // If arr2 does not contain entry from arr1 push it to newArr
      newArr.push(arr1[v]);
    }
  }
    for(var w=0;w<arr2.length;w++){
    if(arr1.indexOf(arr2[w])===-1){
      // If arr1 does not contain entry from arr2 push it to newArr
      newArr.push(arr2[w]);
    }
  }

found someone else using this method in the comments, I think this is close to what I was looking for.

function diffArray(arr1, arr2) {
return arr1.filter(function(elem){
return arr2.indexOf(elem) < 0;
}).concat(arr2.filter(function(elem){
return arr1.indexOf(elem) < 0;
}));
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

1 Like

These are comparing 2 different arrays. You’re running it on the same one.

I was working my way in that direction, if I could see the array responding the way I expected I was going to copy and paste for the 2nd one.

Right, you need to do this before you concat them into a single array.

I’m not sure why they are suppose to be concated at all, but all the tutorials do it as their very first step.

Finally got it in the form I was originally aiming for… I knew I had questions about .filter & .indexOf from a former exercise, getting it in this form helps me understand each peace individually

function diffArray(arr1, arr2) {
  var R1 = [];
  //R1 will contain anything unique in arr1
  R1 = arr1.filter(function(elem) {
    return arr2.indexOf(elem) === -1;
  });
  //console.log(R1);

  var R2 = [];
  //same; but different.
  //R2 will receive anything unique in arr2
  R2 = arr2.filter(function(elem) {
    return arr1.indexOf(elem) === -1;
  });
  //console.log('r2' ,R2);
  //combine both new arrays with concat
  var dfArr = R1.concat(R2);
  
  return dfArr;
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.