I thought I was on the code with this but I realised that the code will output the different elements in a new array TWICE…
Is there no function to check all elements of an array for multiple arrays and compare elements? I tried .includes , if that’s the right one can someone give me a pointer on how to fix this?
Your code so far
function diffArray(arr1, arr2) {
var newArr = [];
for (let i = 0 ; i<arr1.length ; i++) {
if ( (arr1.includes(arr2[i])) === false ) {
newArr.push(arr2[i]);
console.log(newArr);
} else if ( (arr2.includes(arr1[i])) === false ) {
newArr.push(arr1[i]);
}
}
return newArr;
}
console.log(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays/