Diff Two Arrays with `for` loops and `includes()` method


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]);

My browser information:

User Agent is: Chrome/79.0.3945.88.

Challenge: Diff Two Arrays

Link to the challenge:

Why would you push both if only one of them was not in the other array?

hah, indeed). I also tried this awkward construction unsuccessfully:

if (!arr1.includes(arr2[j])){
}else if(!arr2.includes(arr1[i])){

I realise that it is good time to take a pause, but maybe you can help me to close this Gestalt?

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

1 Like

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.