FAQ: Loops - Nested Loops

// Write your code below
let bobsFollowers = ['Jim', 'Joe', 'John', 'Jake'];
let tinasFollowers = ['Jane', 'John', 'Jake'];
let mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++) {
  //console.log(bobsFollowers[i])
  for (let j = 0; j < tinasFollowers.length; j++); {
    //console.log(tinasFollowers[j])
    if (bobsFollowers[i] === tinasFollowers[j]) {
      mutualFollowers.push(bobsFollowers[i]);
    }
  }
}

why do i get this error
iii/main.js:10
if (bobsFollowers[i] === tinasFollowers[j]) {
^

ReferenceError: j is not defined

the semicolon caused the error
can someone explain to me what exactly semicolons do

Semi-colons tell the compiler or interpreter* when something ends. For example:

console.log("I am a programmer");

The ; says that the statement is done.

Take a look at this conditional

const x = 5;

if (x > 10) {
  console.log("x is grater than 10");
} else {
  console.log("x is less than 10");
};

When we declare the variable x we add a Semi-colons saying we finished declaring the variable. Then for each of the console.log statements, we say we finished the statements. And for the last } for the conditional, we use one to say that the conditional is finished.

NOTE: some languages such as JS do not require semi-colons.

*depends on the language for js it is an interpreter.

1 Like

JavaScript is not compiled, but interpreted. The semi-colon is an end of statement token that tells the parser when to stop reading the line. JS is read, interpreted and run one line at a time.

JS does require semi-colons. Other languages such as C, C++, C#, Java, PHP and possibly others also require them, strictly.

1 Like

I thought it was preferred but not require. When I run a js program with no semi-colons it runs with no errors

And I fixed my previous post.

2 Likes

When we leave off the semi-colons, the browser may insert them as needed, however this leads to lazy programming habits that can come back to haunt us.

Today’s browsers and JS engines are much more refined than in the past, even to the point of using a Just In Time compiler (JIT), and covering our back with s/c insertion. However, we should not take this for granted since the script is running on the client and we don’t know how old their browser version is. Best to adopt the practice of using semi-colons where required.

3 Likes

Thank you for your effort, I still don’t see how the comparison between the two arrays are syntactically stated, I will just have to deal with it as it is until I have deeper understanding of Javascript.

const a = ['a', 'b', 'd', 'e', 'g', 'h']
const b = ['b', 'c', 'd', 'e', 'f', 'h']

The outer loop will iterate over, ['a', 'b', 'd', 'e', 'g', 'h']

for (let x of a) {
  console.log(x)
}
a
b
d
e
g
h

On each iteration, we run the full iteration of the other array.

for (let x of a) {
  for (let y of b) {
    if (x === y) {    //  compare two elements
      console.log(x)
    }
  }
}
b
d
e
h

I understand it now, some times I have brain freezes for not apparent reason.
I left for a couple of days looked at it again and it makes complete sense now.
Thank you for your effort anyhow.

2 Likes

Not sure why the lesson isn’t letting me pass when my code worked. Anyone know?

const bobsFollowers = ['tim', 'matt', 'george', 'dan']
const tinasFollowers = ['matt', 'dan', 'susan']

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)

there can’t be any spaces between i and ++ (same for j in the other loop)

Wow it was that simple haha thanks

This way of doing something to each element of an array seems complicated to me, having to grab each one by its index.
What’s a more elegant nice shorthand in JS for doing arbitrary things to each element of an array, first to last?

why when we type:

mutualFollowers.push(tinasFollowers[n])

we get the mutual followers results if we are just pushing tina’s followers into the mutualFollowers array, this operation does not specifically chooses the mutual followers between bob and tina. Can someone explain. I am confused about how this code actually chose the mutual names.

The mutual followers array is a collection of all the entries that appear in both of the other arrays. Which one gets pushed is of no concern since they are both the same, anyway.

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.

Look at the loop condition:

for (let j = 0; i < tinasFollowers.length; j++) {

Thank you! I went over that so many times. Any idea why the error was pointing me to line 3?

That would indicate a runaway loop, perhaps?

This work, but not is correct

// Write your code below
const bobsFollowers = ['Neymar', 'Messi', 'CR7', 'Mbappe']
const tinasFollowers = ['Neymar', 'CR7', 'M. Reus']

let mutualFollowers = []

for(let i = 0; i < bobsFollowers.length; i++) {
  for(let j = 0; j < tinasFollowers.length; j++){
    if(bobsFollowers[i] === tinasFollowers[j]) {
      mutualFollowers = tinasFollowers[j]
      console.log(`Both mutual friends ${mutualFollowers}`)
    }
  }
}