What if I want to seperate looping and match checking?

Hi. I’m a little stuck. I’m working on Nested Loops. I want to separate the loop and the part that checks for matches, so i’ve put them in functions.

Separately, they run fine. But, I can’t figure out how to pass the values back and forth between functions. Here’s the code below , commented as best i can to clarify my thought process. Any help appreciated, thanks.

// Our array variables for later use. 
let bobsFollowers = ['p1', 'p2', 'p3', 'p4'];
let tinasFollowers = ['p1', 'p2', 'tinaspal'];
let mutualFollowers = [];

//Iterates through each index of each array. 
const loopTheArrays = (arr1, arr2) => {
  for (let i = 0 ; i < arr1.length ; i++) {
  for (let j = 0 ; j < arr2.length ; j++) {
    
  }
} };

/* 
Entered to ensure no infinite loop when executing.  Seems to be okay:   
loopTheArrays(bobsFollowers, tinasFollowers); */

// Function to compare array indexes.  
const compareIndexes = (arrayOneIndex, arrayTwoIndex) => {
  if(arrayOneIndex === arrayTwoIndex) {
    mutualFollowers.push(arrayOneIndex);
    	// Is there a way to pass the info from this 					function to the loop, to continue after 				it finds a match and pushes it to mutual 					followers?
  }
};

/* The function calls below are probably wrong. 
I'm trying to run the if loop on each iteration.  So it should loop once, pass to comparison function, check and push if a match, then call the loop function again to continue.  */

compareIndexes(loopTheArrays(bobsFollowers, tinasFollowers));
console.log(mutualFollowers);

That just adds to the code bloat, in this instance. Putting the comparison right in the loop and pushing to the array on matches is much more clear to the reader.

There is nothing to pass since you are pushing to the array. The global object is the result when the function is complete.

Sometimes we don’t want a global object that is manipulated by our function, but rather have our function create, popuplate and return an object.

const find_mutual_followers = (m, n) => {
  result = []
  for (a in m) {
    for (b in n) {
      if (a === b) {       // This is much to simple to
        result.push(a);    // justify creating a function
      }
    }
    return result;
};

const mutual_followers = find_mutual_followers(bobsFollowers, tinasFollowers)

For the sport of it, let’s try to do what your above code is attempting…

const sameAs = (a, b) => a === b;

const find_mutual_followers = (m, n) => {
  result = []
  for (a in m) {
    for (b in n) {
      if sameAs(a, b) {
        result.push(a);
      }
    }
    return result;
}

const mutual_followers = find_mutual_followers(bobsFollowers, tinasFollowers)

Hey MTF, thanks for your time.

So two take-aways from this:

  1. What I’m trying is essentially unnecessary in this case, possibly convoluted.

  2. Looking at your effort to do what the code intended, it’s probably a little outwith my ability atm too?

I’ll refactor the code so it’s inline with what the lesson wants again. This was more of a tangent haha.

1 Like

Maybe so, but it won’t be for long. Just keep pouring over the lessons, and reading as you go. Practice each new concept along the way so you can see more of the in’s and out’s. It all may look simple, but there are some hidden jewels in even the simplest of concepts.

This is what I’m trying to do :slight_smile:

It’s all well and good me learning something in a lesson, but if i don’t practice and continue to build my knowledge of these concepts i’ll be forever amateur.

1 Like