I am trying to write my code with for loops to understand what happens when I iterate through two arrays. I will be happy if somebody explains me my mistake.
function diffArray(arr1, arr2) {
let newArr = [];
for (let i = 0; i < arr1.length; i++){
for (let j = 0; j < arr2.length; j++){
if (!arr1.includes(arr2[j]) || !arr2.includes(arr1[i])){
newArr.push(arr1[i], arr2[j])
}
}
}
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
remember that with if/else if only one of the two execute, but what if you need to push both?
anyway, how many times you want to loop over the whole arr2?
because as it is you are doing it once for each element of arr1
your output may be a bit unexpected
Thank you. I didn’t find the solution with two for loops neither in my mind nor in Internet, so, I am not sure that it is possible (or not tooooo long and ugly). But at least I understood where I stumbled. So, I just wrote pretty and clear code without two for loops, and went ahead. Anyway, it was useful.