pasztora
1/20/2020 - 3:03 PM

Loops in Javascript

// Basic loop for a counter
for (let counter = 5; counter < 11; counter++){
  console.log(counter)
}

// Iterating through arrays (lists)
const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for (let i = 0; i < vacationSpots.length; i++){
  console.log("I would love to visit " + vacationSpots[i])
}

// Writing a nested for loop
let bobsFollowers = ['Joe', 'Marta', 'Sam', 'Erin'];
let tinasFollowers = ['Sam', 'Marta', 'Elle'];
let mutualFollowers = [];

for (let i = 0; i < bobsFollowers.length; i++) {
  for (let j = 0; j < tinasFollowers.length; j++) {
    if (bobsFollowers[i] === tinasFollowers[j]) {
      mutualFollowers.push(bobsFollowers[i]); // mutualFollowers list is filled up with the elements from bobsFollowers if they are present both in bobsFollowers and tinasFollowers
    }
  }
};