Advertisement
karlakmkj

Nested loops in arrays

Dec 9th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let bobsFollowers = ['Mary', 'Jane', 'Tim', 'John'];
  2. let tinasFollowers = ['John', 'Mary', 'Sam'];
  3.  
  4. let mutualFollowers = [];
  5.  
  6. for (let i =0; i < bobsFollowers.length; i++){
  7.   for (let j =0; j < tinasFollowers.length; j++){  //note the use of different variable name j  
  8.     if (bobsFollowers[i] === tinasFollowers[j]){
  9.       mutualFollowers.push(bobsFollowers[i]);  //i is the index(number) hence it should be push bobsFollowers[i] to push the value
  10.     }
  11.   }
  12. }
  13. console.log(mutualFollowers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement