Diff Two Arrays/ Works but Doesn't Work

Diff Two Arrays/ Works but Doesn't Work
0

#1

Tell us what’s happening:

So I’m curious as to why this little snippet of code doesn’t work in the freeCodeCamp editor, however it functions properly in any other console I’ve tried to use? Any ideas? (The error messages don’t make much sense to me).

Thanks!!

Your code so far


function diffArray(arr1, arr2) {
  var newArr = [];
// Same, same; but different.

  function getValues(arr1,arr2){
     var i = 0;
     while (arr1.length > i || arr2.length > i){
       if(arr1.includes(arr2[i]) === false){
         newArr.push(arr2[i]);
       }
       if (arr2.includes(arr1[i]) === false){
         newArr.push(arr1[i]);
       }
       i++;
     }
     console.log(newArr.filter(Boolean));
     return newArr.filter(Boolean);
  }
   
  getValues(arr1,arr2);
} 

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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


#2

I am not sure why this is not passing FCC check.

I removed the second function you’ve created and all worked fine.
Here is what it looked like:

function diffArray(arr1, arr2) {
  var newArr = [];
// Same, same; but different.


     var i = 0;
     while (arr1.length > i || arr2.length > i){
       if(arr1.includes(arr2[i]) == false){
         newArr.push(arr2[i]);
       }
       if (arr2.includes(arr1[i]) == false){
         newArr.push(arr1[i]);
       }
       i++;
     }
     console.log(newArr.filter(Boolean));
     return newArr.filter(Boolean);
 
}

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

#3

Weird! thanks for the input :slightly_smiling_face:


#4

The reason is that the outer function doesn’t return anything, so implicitly returns undefined

If you change it to return getValues(arr1, arr2) it should have worked


#5

Ohhh I get it, wow such a rookie mistake. Thanks!