Tell us what’s happening:
I am trying to splice off common elements in the each of the arrays, and then concatenate them to get the symmetric difference of the two arrays.
However, I discovered that my code is unable to run the second nested for-loop to get my desired result. The problem goes away when I comment out the first nested for-loop.
Your code so far
function diffArray(arr1, arr2) {
var longerArray = [];
var shorterArray = [];
if (arr1.length > arr2.length){
longerArray = arr1;
shorterArray = arr2;
} else {
longerArray = arr2;
shorterArray = arr1;
}
//finding A' - 1st nested for-loop
for(let i = 0; i < longerArray.length; i++){
for(let j = 0; j < shorterArray.length; j++){
if(longerArray[i] === shorterArray[j]){
longerArray.splice(i,1)
console.log(longerArray);
}
}
}
console.log(longerArray);
//finding B' -2nd nested for-loop
for(let x = 0; x < shorterArray.length; x++){
for(let y = 0; y < longerArray.length; y++){
if(shorterArray[x] === longerArray[y]){
shorterArray.splice(x,1)
console.log(shorterArray);
}
}
}
console.log(shorterArray);
var newArr = [];
newArr = longerArray.concat(shorterArray);
console.log(newArr);
// Same, same; but different.
return newArr;
}
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36
.
Link to the challenge: