Intermediate Algorithm Scripting - Diff Two Arrays Two for loops

Tell us what’s happening:
why am I getting just two arrays Concatenation instead of difference ?

Your code so far

function diffArray(arr1, arr2) {
  let a = [];
  for( let i=0;i<arr1.length;i++){
    let bool=false;
    for(let j=0;j<arr2.length;j++){
      if (arr1[i]!==arr2[j]){
        bool=true;
      }
    }
    if(bool){
      a.push(arr1[i]);
    }
  }
  for( let i=0;i<arr2.length;i++){
    let bool=false;
    for(let j=0;j<arr1.length;j++){
      if (arr2[i]!==arr1[j]){
        bool=true;
      }
    }
    if(bool){
      a.push(arr2[i]);
    }
  }
  return a;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
**Challenge:** Intermediate Algorithm Scripting - Diff Two Arrays

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays

bool will be true unless arr1[i] matches the last item in array2. This is because you are checking every value of arr2 and setting the value of bool on each check.

1 Like

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