5/7 for loops inside of for loops: don't understand how a certain line works


#1
So I got the code to work perfectly, I'm just confused about HOW it works. Specifically, in the if statement, I don't understand why myPlaces[i] is being printed to the console. Why does printing that give you the SPECIFIC place that matches in the two arrays? I understand that the if statement is asking if there are any of the same items in the two arrays, but to me, it would seem like printing myPlaces[i] to the console would just reprint the entire myPlaces array if the "if" statement were true, not the specific place that matches. Sorry if this is confusing, but if someone understands what I'm saying, please respond. Thank you.

https://www.codecademy.com/courses/learn-javascript/lessons/loops/exercises/for-loops-iii?action=lesson_resume&link_content_target=interstitial_lesson



var myPlaces = ['Sweden', 'Norway', 'Ireland']
var friendPlaces = ['Scotland', 'Sweden', 'Denmark']
for(var i = 0; i < myPlaces.length; i++) {
  for(var j = 0; j < friendPlaces.length; j++) {
    if(myPlaces[i] === friendPlaces[j]) {
      console.log('Match: ' + myPlaces[i])
    } else {
    }
    }
  } 



#2

Maybe reading
== the Book ==
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
and
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

Let me know…


#4

Try using this more verbose script…

var myPlaces = ['Sweden', 'Norway', 'Ireland'];
var friendPlaces = ['Scotland', 'Sweden', 'Denmark'];
console.log("Start 1st FOR loop");
for(var i = 0; i < myPlaces.length; i++) {
  console.log("item-"+i+" from myPlaces is "+myPlaces[i]);
  console.log("    ==== Start 2nd FOR loop");
  
  for(var j = 0; j < friendPlaces.length; j++) {
    console.log("       item-"+j+" from friendPlaces is "+friendPlaces[j]);
    console.log("       check "+myPlaces[i]+" EQUALS "+ friendPlaces[j]);
    if(myPlaces[i] === friendPlaces[j]) {
      console.log('     Match: ' + myPlaces[i]);
    } else {
      console.log("       NoMatch");
    }
    console.log("    ==== goto NEXT item in 2nd FOR loop");

    }
    console.log("    ==== NO MORE item in 2nd FOR loop");
  console.log("====goto NEXT item in 1st FOR loop");
  }
  console.log("==== NO MORE item in 1st FOR loop");

#5

Hi @cssplayer03499,

Everyone is trying to learn and help each other here. You’ve spent your time to construct your question and explain your doubts here, other people too, spent their time reading your question and hopefully providing an explanation and solution to your question, then why not make this happen with some positivity? :thinking:

The short answer is : No. The fact that you mentioned: …it would seem like printing myPlaces[i] to the console would just reprint the entire myPlaces… , might suggest that you still haven’t fully understand about the subject matter itself.

Therefore, what @leonhard.wettengmx.n suggested is that, you may need to revisit the For loop and Array topic to read more about it, I know it may seem too much reading, but if you’re considering learning coding by yourself, you need to know that this is just a beginning, there are more readings to come.


For your question:

Firstly, I need to comment on your code:

for(var i = 0; i < myPlaces.length; i++) {
  for(var j = 0; j < friendPlaces.length; j++) {
    if(myPlaces[i] === friendPlaces[j]) {
      console.log('Match: ' + myPlaces[i])
    } else {                               //<===== else is not needed here, since you have no running code for Else, it is redundant to write an else statement with empty execution block
    }
    }
  }

So it becomes:

for(var i = 0; i < myPlaces.length; i++) {
  for(var j = 0; j < friendPlaces.length; j++) {
    if(myPlaces[i] === friendPlaces[j]) {
      console.log('Match: ' + myPlaces[i])
    } 
  }
}

So now I will explain it step by step, using the for loop code:

The 1st loop initiated: (First round), when i = 0, j = 0,

// When i = 0, myPlaces[0], which referred to the myPlaces array, 'Sweden'

for(var i = 0; i < myPlaces.length; i++) {          //<===== when 1st loop start, it starts at i=0 
  for(var j = 0; j < friendPlaces.length; j++) {    //<====== when i=0, it starts the 2nd loop, 2nd loop starts at j=0,
    if(myPlaces[i] === friendPlaces[j]) {           //<====== if myPlaces[0] === friendPlaces[0], 'Sweden' vs 'Scotland'
      console.log('Match: ' + myPlaces[i])          //<===== Since 'Sweden' vs 'Scotland' is not equal, therefore the console.log won't run, the code skips this and go to the next code
    }          //<===== IMPORTANT part here: End of if statement, continue the 2nd loop, j++ means now the j=1, and the 2nd loop continue to repeat from top down.
  }            //<===== End of the 2nd loop, j finish run from 0 to 2 (<friendPalces.length), continue to run 1st loop, i++, means only now i = 1,
}              //<===== Closing 

When the 2nd loop start to run again, this time when i (still) = 0, but j = 1,

// When i = 0, myPlaces[0], 'Sweden'

for(var i = 0; i < myPlaces.length; i++) {          //<=====  i still at 0 
  for(var j = 0; j < friendPlaces.length; j++) {    //<====== 2nd loop starts at j=1 this time,
    if(myPlaces[i] === friendPlaces[j]) {           //<====== if myPlaces[0] === friendPlaces[1], 'Sweden' vs 'Sweden'
      console.log('Match: ' + myPlaces[i])          //<===== Since 'Sweden' vs 'Sweden' is equal and matched, therefore the console.log will run, console.log get printed.
    }          //<===== IMPORTANT part here: End of if statement, continue the 2nd loop, j++ means now the j=2, and the 2nd loop continue to repeat from top down.
  }            //<===== End of the 2nd loop, j finish run from 0 to 2 (<friendPalces.length), continue to run 1st loop, i++, means only now i = 1,
}              //<===== Closing 

To summarize,

  1. When 1st loop initiated, i = 0 starts at the 1st loop, continue to the 2nd loop, j will run from 0 to 2,
  2. Then only return to 1st loop i = 1, then continue to 2nd loop, j will run again from 0 to 2,
  3. Then return back to 1st loop i = 2, to continue to 2nd loop, j will run again from 0 to 2.

@leonhard.wettengmx.n had provided a good code for you to make the observation yourself

I wonder you’d appreciate it as much or whether you’re hoping for more people to provide the answer you’ve been waiting for. Anyhow, I think you owe @leonhard.wettengmx.n to edit your reply, it seems a bit unappreciative for his time and effort in helping you out.

Just my opinion tho. :hugs:


#6

Okay I understand now, thank you. I apologize to @leonhard.wettengmx.n for what I said. The info you gave me at first just seemed very vague and I didn’t know what to read. I guess I was just frustrated and took it out on you so I’m sorry.


#8