Reputation:
I want to store the common strings from bobsFollower
array and tinasFollower
array in mutualFollowers
array.
const bobsFollowers = ['vinit','vidyesh','bipin','shobhana'];
const tinasFollowers = ['vinit','vidyesh','manish'];
const mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++){
for (let j = 0; j < tinasFollowers.lenght; j++){
if (bobsFollowers[i] === tinasFollowers[j]){
mutualFollowers.push(tinasFollowers[j]);
}
}
};
console.log(mutualFollowers);
inner for loop in not getting executed
Upvotes: -1
Views: 74
Reputation: 115
Seems like you are misspelling length as lenght on the 5th line. and also there is an extra semicolon on the 10th line, which is not causing the error. Here is the solution.
const bobsFollowers = ['vinit','vidyesh','bipin','shobhana'];
const tinasFollowers = ['vinit','vidyesh','manish'];
const 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]);
}
}
}
console.log(mutualFollowers );
Upvotes: 0
Reputation: 139
Much easier with filter
const bobsFollowers = ['vinit','vidyesh','bipin','shobhana'];
const tinasFollowers = ['vinit','vidyesh','manish'];
const mutualFollowers = bobsFollowers.filter(element => tinasFollowers.includes(element));
console.log(mutualFollowers);
Upvotes: 0
Reputation: 182
you made a mistake in your code : in the second "for" you wrote "tinasFollowers.lenght" instead of "tinasFollowers.length" ;)
Here is the correct code :
const bobsFollowers = ['vinit','vidyesh','bipin','shobhana'];
const tinasFollowers = ['vinit','vidyesh','manish'];
const mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++){
for (let j = 0; j < tinasFollowers.length; j++){
if (bobsFollowers[i] === tinasFollowers[j]){
mutualFollowers.push(tinasFollowers[j]);
}
}
};
console.log(mutualFollowers);
Upvotes: 0
Reputation: 797
You had a typo in the inner loop: you mistyped length
as lenght
!
const bobsFollowers = ['vinit','vidyesh','bipin','shobhana'];
const tinasFollowers = ['vinit','vidyesh','manish'];
const mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++){
for (let j = 0; j < tinasFollowers.length; j++){
if (bobsFollowers[i] === tinasFollowers[j]){
mutualFollowers.push(tinasFollowers[j]);
}
}
};
console.log(mutualFollowers);
Upvotes: 1