Intermediate Algorithm Scripting - Diff Two Arrays

Tell us what’s happening:

Describe your issue in detail here.
Good day, I’m quite confused as to why my code doesn’t work. I looked at the solutions on the page and solution one was very similar to the code. The only difference was the use of
“function OnlyInFirst (first, second){}” and variables accordingly. Im confused as to why this makes a difference and what difference it does make.
Kind regards

Your code so far

function diffArray(arr1, arr2) {
  const newArr = [];
  for( let i = 0; i < arr1.length; i++){
    if(arr2.indexOf(arr1[i] === -1)){
      newArr.push(arr1[i]);
    }
  }
  return newArr;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15

Challenge Information:

Intermediate Algorithm Scripting - Diff Two Arrays

you should check if arr1[i] exists in arr2 .
but the code is checking if arr2 contains the boolean result of arr1[i] === -1 ,

  1. Check the use of indexOf again.

Your code:

if(arr2.indexOf(arr1[i] === -1))

First of the examples:

if (second.indexOf(first[i]) === -1)
  1. That solution has an inner function that runs twice to compare both arrays to each other.

Your ‘solution’ actually differs in other important ways from the given solution to which you are referring. It’s not really helpful to copy and paste solutions, especially if you don’t understand how or why they work. It’s far better to try to come up with your own solution and ask for help if you’re stuck.

Do you understand exactly what your current code is doing, line by line?

2 Likes

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