5/7 Matching our lists


#1

I completed the exercise as instructed and got my green check of approval, but I'm just curious why my output was "undefined." I tried rearranging the order to make sure they lined up, but that didn't resolve in any match. Any thoughts?

var myPlaces = ['Earth', 'Wind', 'Fire'];
var friendPlaces = ['Earth', 'Venus', 'Mars'];
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]);
}
//output Match: undefined


#2

Hi @scriptrunner81653,

You'd need the if statement to be inside of your second loop (under the friendPlaces loop)

Insert that if statement into that second loop.
Then, run the code and see the results. :slight_smile:


For something neat, you can consider deleting the console.log(friendPlaces[j]);, so that the code would only print the match it found in friendPlaces and also printing myPlaces.

For something much neater, you can consider deleting the console.log(myPlaces[i]); and console.log(friendPlaces[j]); so that it would only print the match found between the two variables.

You can tinker with the code to try it.


#3

Thanks @codexthon
I was tinkering earlier and experimenting on cleaning things up. I've been finding that as long as your equations (that's how I think of JavaScript) are sound, then you can play around with them all you want.
Problem solved. Thanks again. Glad I wasn't crazy for thinking I could re-write the code.


#4

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