-1

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

1
  • 5
    Is tinasFollowers.lenght supposed to be .length? Commented Apr 18, 2020 at 12:11

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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);

Comments

0

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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.