Diff Two Arrays : The most basic solution?

Hey there!

I solved this problem finally, but I just wanted to know a few things… (removed some questions because they turned out to be user error :P)

  1. Wouldn’t this example be a more ‘basic’ solution for ‘get a hint’?
  2. Any advice on better formatting is welcomed. Thank you!! :smiley: :smiley: this site is very empowering so far.

Your code so far


function diffArray(arr1, arr2) {
   let concatArr = arr1.concat(arr2);
   concatArr.sort((a,b) => a > b);
   let finalArr = [];
     for(let i = 0; i < concatArr.length;i++) {
       let splitArr = [...concatArr];
       splitArr.splice(i,1);
         if(splitArr.indexOf(concatArr[i]) < 0) {
           finalArr.push(concatArr[i]);
         }  
   }
  return finalArr;
  }
   
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; rv:63.0) Gecko/20100101 Firefox/63.0.

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

Right now you have loc = i, if you chance nothing else it is normal that it doesn’t work if you assign loc = concatArr[i]

You can certainly remove some unnecessary assignments, for example from above you can just use i and remove the use of loc. At one point you use concatArr[i] and an other time you use concatArr[loc], but they are the same thing. Coherence is certainly an important thing for readability

Also, instead of testNum you can just use concatArr[i]

1 Like

Ok, I probably missed something, because I tried again and at least removing ‘loc’ worked, thank you!

(still learning)