Which variable to use in end result while comparing with nested loops

Hi,

To get the comparison working in example 1 should use the outer loop variable and in example 2 the inner loop variable. Does someone know why?

Thanks a lot!

Cheers,

Ricardo

Example 1

let bobsFollowers = ['Joe', 'Marta', 'Sam', 'Erin'];
let tinasFollowers = ['Sam', 'Marta', 'Elle', 'Ricardo'];
let 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);

Example 2

const myArray = [6, 19, 20];
const yourArray = [19, 81, 2];
for (let i = 0; i < myArray.length; i++) {
  for (let j = 0; j < yourArray.length; j++) {
    if (myArray[i] === yourArray[j]) {
      console.log('Both loops have the number: ' + yourArray[j])
    }
  }
};
2 Likes

Example 1 works with the mutualFollowers array, so it loops through both of the basic followers array, and if they have the same follower that name will be put (or copied) into the mutualFollowers
And once it looped through both arrays we will have a complete mutualFollowers array filled with their same followers.

But example 2 works the same unless that it doesn’t have an array where it stores the same numbers.
It simply logs the same values to the console (one at a time).

I would recommend using example 1.
(if this is what you meant)

Hope this helps :grinning:

1 Like

In both cases, you could use the value from either array. (The array iterated through by either the inside loop or the outside loop.) In your if statement you’ve determined that the values are the same, so it doesn’t matter which one you either .push() or console.log(). You would get the same result.

2 Likes

Thanks lot lolfail and midlindner!

I still have a question :wink: When I use in example 1 mutualFollowers.push(bobsFollowers[i]); I get the (right) result:

[ ‘Marta’, ‘Sam’ ]

When I change bobsFollowers into tinasFollowers I get the following result:

[ ‘Marta’, ‘Elle’ ]

When I change in example 2 yourArray into myArray in the console log I get ‘Both loops have the number: 6’ instead of ‘Both loops have the number: 19’.

I would say both arrays while using either the .push or console log method should lead to the same result, but appearantly the script has another logic :wink: It confuses me that example 1 uses the outer loop variable (bobsFollowers) and in example 2 the inner loop variable (yourArray). Hopefully you can help.

Thanks a lot!

Ricardo

1 Like

Yes, it gives you the same result, but as much as I could guess you didn’t change i to j, it won’t work well because j belongs to tinasFollowers and i belongs to bobsFollowers:

What you probably did:

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

How it works:

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

Hope this helps :grinning:

1 Like

Thanks a lot for your quick reply! That certainly helps :wink:

1 Like

Complete Aside

While it is fine and good to have verbose variable names, I cringe everytime I see them in code. My compromise is to allow them at one end of a transaction, but not both.

bobsFollowers = [];     // pretend it's populated
tinasFollowers = [];    // ditto
mutualFollowers = mutual(bobsFollowers, tinasFollowers);
function mutual(a, b) {
  let p, q, m = [];
  for (p of a) {
    for (q of b) {
      if (p === q) {
        m.push(p);
      }
    }
  }
  return m;
}

The verbiage is fine in the outer layers where we are expecting to know what we are looking at. Inside the code, that much is not required. Plain symbols is enough. The reader can see what was passed in and the description the return value is given at the caller. Wordy enough, for my liking.

Opinion, as it were. Sure helps reduce typing errors.

As for documenting this code, it would be foolhardy. It explains itself so is clearly self-documented.


Also Aside

Use let in functions and leave const to the globals. Inside a function we control everything and having a constant is meaningless. All we want is to keep our variables from leaking into global scope. let does that in any block. A function body is a block.

const PI = Math.PI;
const pi = +(PI.toFixed(4))
 > PI
<- 3.141592653589793
 > pi
<- 3.1416

All our code can call upon (poll) these two variables.