I’m getting an error for line 3 (const tinasFollowers), and I can’t figure it out.
// Write your code below
const bobsFollowers = [‘John’, ‘Sally’, ‘Fred’, ‘Scooby’];
const tinasFollowers = [‘Ginger’, ‘Fred’, ‘Sally’];
const mutualFollowers = ;
for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; i < tinasFollowers.length; j++) {
if (bobsFollowers[i] === tinasFollowers[j]) {
mutualFollowers.push(tinasFollowers[j]);
}
}
}
console.log(mutualFollowers);
Here is the error:
/home/ccuser/.bin/node: line 3: 248 Killed /usr/bin/node --max-old-space-size=200 $@
It always says line 3, but the # killed changes each time.
My first attempt included an incorrect semicolon at the end of this line: if (bobsFollowers[i] === tinasFollowers[j]); {
Which resulted in three copies of each element in the first array (bobsFollowers) being pushed to the empty array, including the one non-mutual element.
Removing the semicolon passed the challenge, but I do not understand how that extra semicolon led to the result it did. Can anyone provide an explanation?
The semi-colon token tells the script parser where the end of statement is. Once it reaches that token, it quits parsing and begins interpreting and compiling the code it has read in to that point.
We can see that an if statement has a code block which can run into several lines of code. The statement does not end until the last closing brace, so it follows we would not want any semi-colons outside of the block(s), either before or after.
if (bobsFollowers[i] === tinasFollowers[j]) {
mutualFollowers.push(bobsFollowers[i]);
}
If the condition is true, the code block between the curly braces is executed i.e. if the condition is true, an element of bobsFollowers is pushed to the mutualFollowers array. If the condition is false, then nothing happens.
Your code (with incorrect semi-colon):
if (bobsFollowers[i] === tinasFollowers[j]); { // Incorrect semi-colon
mutualFollowers.push(bobsFollowers[i]);
}
// IS EQUIVALENT TO
if (bobsFollowers[i] === tinasFollowers[j]) {
// Empty block
}
{ // Separate block not dependent on any condition
mutualFollowers.push(bobsFollowers[i]);
}
There are 4 elements in bobsFollowers and 3 elements in tinasFollowers. You have a for loop iterating over bobsFollowers and a nested for loop iterating over tinasFollowers.
In every iteration of the outer for loop, the inner for loop runs through three iterations. The if condition is evaluated, but because of the incorrect semi-colon, regardless of whether the condition is true or false, no block of statements is executed. The block of code with the push statement is not a part of the if statement, but a completely separate block. So, in each iteration an element of bobsFollowers is pushed to mutualFollowers resulting in three copies of each element of the outer loop being pushed into the mutualFollowers array.
When I have two arrays with differents lengths, should I put the larger in the outher loop? And the result of the same followers should be made considering the larger array?
It should not matter which loop the larger array is iterated in given that all the values will be accessed, regardless of order. It will not have any effect on the Big O evaluation of the code (n^2, either way).
Hi:sweat_smile: could someone please explain why i’m getting the values 1 & 2 in my console? i’ve been going over my code for ages trying to figure out what i’ve done wrong and my mind is blanking!! thank u in advance
Your console.log statement nested inside the loops is printing the return value of the push method.
The documentation of a programming language is a very useful resource to learn more about the parameters, return values and details about methods and other features of the language.