xxxxxxxxxx
// Write your code below
let bobsFollowers = ["Ikubie","FK","Jola","Peter"];
let tinasFollowers = ["ISWIS","FK","Jola"];
let mutualFollowers = [];
//Loop through bob's followers
for (let i=0;i<bobsFollowers.length;i++) {
//Loop through tina's followers
for (let j=0;j<tinasFollowers.length;j++) {
//If Followers are the same
if (bobsFollowers[i] === tinasFollowers[j]) {
//Add the element to the empty array
mutualFollowers.push(bobsFollowers[i]);
}
}
}
console.log("Mutual Followers: " + mutualFollowers);
xxxxxxxxxx
var arr = [[1,2], [3,4], [5,6]];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
xxxxxxxxxx
// Basic Javascript Nested Loop / 2D Array
for (var x = 0; x < 10; x++) {
for (var y = 0; y < 10; y++) {
console.log("x: " + x + ", y: " + y);
}
}
xxxxxxxxxx
//nested for loop
var arr = [[1,2], [3,4], [5,6]];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
xxxxxxxxxx
A nested loop in JavaScript is a loop that is inside another loop. The inner loop will execute its code block for each iteration of the outer loop. Here is an example:
for (let i = 1; i <= 3; i++) {
console.log("Outer Loop: " + i);
for (let j = 1; j <= 2; j++) {
console.log("Inner Loop: " + j);
}
}
In this example, the outer loop will iterate 3 times, and for each iteration of the outer loop, the inner loop will iterate 2 times. So the output will be:
Outer Loop: 1
Inner Loop: 1
Inner Loop: 2
Outer Loop: 2
Inner Loop: 1
Inner Loop: 2
Outer Loop: 3
Inner Loop: 1
Inner Loop: 2
Note that, the inner loop will execute its code block for each iteration of the outer loop and the inner loop will execute its code block for each iteration of the outer loop.
xxxxxxxxxx
for (let exercise = 1; exercise <= 3; exercise++) {
console.log(`---------- Starting exercise ${exercise}`)
for (let rep = 1; rep <= 6; rep++) {
console.log(`Exercise ${exercise}: Lifting weight repetitition ${rep}`);
}
}
xxxxxxxxxx
var arr = [[1,2], [3,4], [5,6]];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}