Ok, so I want to return the elements that are present in both provided arrays having the same index location.
exe1 = [A,B,C,D,N]
exe2 = [B,D,C,A,T]
it should return only C
I’ve tried looping them by nested loops but doesn’t work, here is what I’ve tried:
let testing = []
for (let i = 0; i < exe1.length; i++){
for(let j = 0; j < exe2.length; j++){
if(exe1[i] === exe2[j]){
testing.push(exe1[i])
}
}
};
return testing;
mind the names of the arrays, please
>Solution :
You can use a simple filter to only include values in exe1 that have the same value at the same index of exe2.
const exe1 = ['A','B','C','D','N'];
const exe2 = ['B','D','C','A','T'];
const testing = exe1.filter((val, i) => val === exe2[i]);
console.log(testing)