Intermediate Algorithm Scripting - Diff Two Arrays

Tell us what’s happening:

So I believe I am on the right track. and I am getting “pink wool” which is the element in arr1 that is not in arr2. However, I do not fully understand line 4 (line with the let). Can someone help me understand how the logic works? Particularly I do not understand “!arr2.includes(element)” cause I thought .includes returns a true or false? Also I thought filter returns everything that meets the criteria in the parentheses? Can someone help me understand the logic in line 4 step by step please?

Your code so far

function diffArray(arr1, arr2) {
  
  // all the elements in arr1 that are not in arr2
  let arr1Filter = arr1.filter(element => !arr2.includes(element));
  console.log(arr1Filter);
}

diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"],["diorite", "andesite", "grass", "dirt", "dead shrub"]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

Challenge Information:

Intermediate Algorithm Scripting - Diff Two Arrays

Hi @fernandez2017

The .filter method is looking at each item in arr1
!arr2.includes(element) is making a check.
If the item is not in arr2 return True, otherwise return False

Only items which have a truthy value are returned in the new array.

Happy coding

1 Like

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