Why does this solution have two functions? Why not just fit it all into the first one?
Your code so far
function diffArray(arr1, arr2) {
const newArr = [];
function onlyInFirst(first, second) {
// Looping through an array to find elements that don't exist in another array
for (let i = 0; i < first.length; i++) {
if (second.indexOf(first[i]) === -1) {
// Pushing the elements unique to first to newArr
newArr.push(first[i]);
}
}
}
onlyInFirst(arr1, arr2);
onlyInFirst(arr2, arr1);
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/113.0.0.0 Safari/537.36
Challenge: Intermediate Algorithm Scripting - Diff Two Arrays
Link to the challenge: