Nested Loop - Mutual Followers Exercise

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-loops/lessons/loops/exercises/for-loops-iii

Hello Everyone,

I was solving this problem above and entered the following code -

let bobsFollowers = [‘omar’,‘tom’,‘fred’,‘binny’];
let tinasFollowers = [‘tom’,‘jill’,‘fred’];
let mutualFollowers = ;

for(let x = 0; x < bobsFollowers.length ; x++){
for (let i = 0; i < tinasFollowers.length ; i++){
if (bobsFollowers === tinasFollowers[i]){
return mutualFollowers.push(tinasFollowers[i]);
}
}
};

console.log(mutualFollowers);

I get a response that says " Syntax Error - Illegal Return Statement’

Why is this an illegal return statement? If you remove the return word it works fine, but I want to know why it is illegal to use return there. Thank you to anyone who can help.

Hello @board7501978669, welcome to the forums! return is used to exit a function, and pass data back to the caller. Keeping this in mind, you can only use the return keyword in functions. Do you your return inside a function?

2 Likes