i am trying to find the intersection of the elements using nested for loop. I made several mistakes doing so.
- I dont know why arrOfArrs[i+1] is not applicable
- I dont understand how will i compare the resulting array to the third array after the first iteration
Hypothetically, if the code had worked, wouldnt it just compare array1 to array2 push the result into resultArray, and same thing for array2 to array3 and finally push the result.
Please share your insight on this. Much obliged!
function intersection(arrOfArrs){
let resultArray = [];
let nextArray = 0;
for(let i = 0 ; i < arrOfArrs.length; i++ ){
for(let j = 0; j < arrOfArrs[i].length; j++){
const currEl = arrOfArrs[i][j];
if(arrOfArrs[(i+1)].length){
if(arrOfArrs[(i+1)].includes(currEl)){
resultArray.push(currEl);
}
}
}
}
return resultArray;
}
const arr1 = [5, 10, 15, 20];
const arr2 = [15, 88, 1, 5, 7];
const arr3 = [1, 10, 15, 5, 20];
console.log(intersection([arr1, arr2, arr3])); // should log: [5, 15]