Intermediate Algorithm Scripting - Diff Two Arrays

Tell us what’s happening:
I have a doubt with the commented part
I’m unable to figure out the use of it
I want a detailed explanation of how it is working…

Your code so far

function diffArray(arr1, arr2) {
  const result = [];
  const items = new Set([...arr1, ...arr2]);
    items.forEach(item=> {
      if(!arr1.includes(item) || !arr2.includes(item)){  //Here, what is necessity of ! operator?
      result.push(item);
      }})
      return result;
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Link to the challenge:

It’s easier to explain this solution if you already have a solution of your own. Can you share your solution?

If you don’t have a solution yet, I’d start there. Reading code is an important skill,when it doesn’t really help you learn how to write code.

I didn’t solved it yet!

could you please explain that specific commented part for me

No, I don’t really enjoy explaining solutions to people when they haven’t solved the problem yet. I don’t think its very productive and I honestly don’t enjoy the process.


I’d be happy to help you work on your own code for the problem though.

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