Solution not accepted: 5 for loops inside of for loops


#1


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


Hoping someone can help me - code is working correctly but I can't move on with lesson.
My code produces a list of comparisons (using if/else in a nested for loop), and correctly identifies the match - Spain. But i still get this error:
Did you write an if/else statement inside the second for loop?


var myPlaces = ['Spain', 'Australia', 'South America'];
var friendPlaces = ['Florida', 'Hawaii', 'Spain'];

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


#2

Read the instructions once more and place your

in the correct position in your code.


#3

Thanks for the help, I've done what you said and the console.log is now within the first for loop. But I'm still getting the same error. just to clarify, I'm ONLY getting an error on task 6.

var myPlaces = ['Spain', 'Australia', 'South America'];
var friendPlaces = ['Florida', 'Hawaii', 'Spain'];

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

#4

What does the Instruction tell you to do.....


#5

Here are the instructions on task 6:

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.


#6

I've also looked at the Hint for this instruction, which shows exactly the wording I use above.

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

I've tried removing the other console logs, removing the else statement, but I can't get this solution to be accepted.


#7

Ok I re-did this page 3 times and finally the last time was accepted. I still don't know what I did wrong before. If you can point it out to me, I would really appreciate it.

var myPlaces = ['Spain', 'Australia', 'South America'];
var friendPlaces = ['Florida', 'Hawaii', 'Spain'];

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

#8

I'm stuck here, too. Anyone with the correct codes please post so I can move on to the next lesson. Thanks.


#9

got it figured out. In else you need a {} like this else { } {console.log('not a match.');}}


#10

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