5. For loop inside of for loop


#1


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

Basically I have to:
"Now, inside of the existing for loop's block, write another for loop that loops over friendPlaces. This time, instead of using the i as the variable name, use j, so we don't overwrite any variables."
, however I get this error "Did you a for loop that loops over the friendPlaces array?" . I checked my code a couple of times and I do not seem to find where I get it wrong. Can someone please explain to me what I'm doing wrong.


var myPlaces = ['Peru', 'USA', 'Japan'];
var friendPlaces = ['Argentina', 'Chaina', 'USA'];
for (var i = 0; i < myPlaces.length; i++) {
  console.log(myPlaces[i]);
  for (var j = 0; j < friendPlaces.lenght; j++) {
    console.log (friendPlaces[j]);
    
  }


}


#2

So you wrote two for loops that both printed their list. The instructions wanted you to write afor loop for friendPlaces that actually prints if there is a math between the two lists. Everything you have is perfect, but after

you want to "Inside the second for loop's block, write an if/else statement that checks if myPlaces[i] is equal to friendPlaces[j]. If it is, log to the console the place you have in common." as per the instructions. What this means is that you want to write an if statement with the condition checking whether or not the lists have any equal terms so you would use the, === operator.
Inside of the if statement, if they math then you want to print "Match: " and then concatenate myPlaces[i] inside of the console.log statement. I hope this helps.


#3

Thank you for your reply!

I am still at the stage of the exercise where I need to:

You logged all of your places! Now, inside of the existing for loop's block, write another for loop that loops over friendPlaces. This time, instead of using the i as the variable name, use j, so we don't overwrite any variables.

In my mind I think that the console should log the second for loop which is within the main for loop for myPlaces but apperantly there's an error with the code. What I get logged into the system is just the list from myPlaces, and unfortunately I still do not get why and where I'm doing something wrong.


#4

Actually, the code you posted is perfect. You just need to add onto it the if statement inside your second for loop. :slight_smile:


#5

Thank you. Apart from my obvious typo with China I changed the code a bit and added the "if" statement.

The code that worked and let me pass the exercise is:

var myPlaces = ['Peru', 'USA', 'Japan'];
var friendPlaces = ['Argentina', 'China', 'USA'];

for (var i = 0; i < myPlaces.length; i++) {
console.log(myPlaces[i]);

for (var j = 0; j < friendPlaces.length; j++) {
console.log(friendPlaces[j]);

if (myPlaces[i] === friendPlaces[j]) 
{
  console.log('Match:' + myPlaces[i]);

}

}
}

Is that supposed to be correct because the system logs all of the countries in the two lists?


#6

Yep! That's perfect. The exercise wanted you to log each places list AND it wanted you to log if they had a match so that you know how to do it in the future. If you wanted just the match though, you would just take away the two

lines and it would just log if there was a match, (just for future reference!)

I'm glad I could help. :smile:


#7

Once again, thank you very much for the help! :relaxed:


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.