Javascript, 'for loops inside for loops '


#1

I’ve been encountering a bug . Though the required output gets executed , i still get an error .
its " Did you write an if/else statement inside the second for loop?"
here’s the code :

var myPlaces = ['UAE ’ , ‘Singapore’ , ‘Hyderabad’];

var friendPlaces = [‘Canada’ , ‘Malaysia’ , ‘Hyderabad’];

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]);
}
}
}

and here’s the output :

UAE
Canada
Malaysia
Hyderabad
Singapore
Canada
Malaysia
Hyderabad
Hyderabad
Canada
Malaysia
Hyderabad
Match:Hyderabad

the main purpose of this program is to check whether there is a match , and as you can see from the output , it does show a match but still shows an error . Any ideas why this is so ?


#2

I think on this one it only wants you to log the name of the place that matched. Try just removing the “Match:” part of your string and just have it report the match like so:

console.log(myPlaces[i]);


#3

Thanks for the reply .But i still get an error when i remove “Match:” .
And as a matter of fact , the hint section itself has it .


#4

Did you also follow this step in the instructions?

“Delete the two console.log() statements you wrote in the previous steps.”


#5

Is that step in the same section ?


#6

As best I can tell without a direct link to the section you’re discussing, I believe you’re on Step 5 of this lesson, with these instructions:

"Look what printed to the console. Your first place printed, then all three of your friend’s. Then your second place, then your friend’s places again. Finally, your third place printed and all three of your friend’s places printed a final time.

This is because the inner for loop runs completely every time the outer for loop runs once.

The purpose of the program is to see what you and your friend have in common. Let’s utilize the === comparison with an if statement.

Delete the two console.log() statements you wrote in the previous steps.

Inside the second for loop’s block, write an if statement that checks if myPlaces[myPlacesIndex] is equal to friendPlaces[friendPlacesIndex]. If it is, log to the console the place you have in common."


#7

Yeah , thats the one.
But that statement of deleting the console.log statements is not there .
I’ve copied the instructions below.

"Look what printed to the console. Your first place printed, then all three of your friend’s. Then your second place, then your friend’s places again. And then one more time.

This is because the inner for loop runs completely every time the outer for loop runs once. Click ‘Run’ one more time to proceed.

The purpose of the program is to see what you and your friend have in common. Let’s utilize the === comparison with an if/else statement.

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."


#8

Im so done coding hahaha
tbh i had a hunch about the brackets and i was messing with them when suddenly i saw a tick.
just to let you know here’s the code

var myPlaces = ['UAE ’ , ‘Singapore’ , ‘Hyderabad’];

var friendPlaces = [‘Canada’ , ‘Malaysia’ , ‘Hyderabad’];

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]);
}

}

and here’s the output

UAE
Canada
Malaysia
Hyderabad
Singapore
Canada
Malaysia
Hyderabad
Hyderabad
Canada
Malaysia
Hyderabad


#9

Also one quick question , why isnt 'Match '+ myPlaces[i]) getting executed ?


#10

Hi @zaidtechnologist,

For anyone to help you better, it is very advisable to:

  • Post a link to the lesson (confusion might happen if you want anyone to search for the lesson/topic of yours as there might be different version of exercises.)
  • State the error the lesson throws (You’ve done that, thank you)
  • Use proper markdown to format your code in your post accordingly (use the </> icon to wrap your code), you can refer more here.

That way, you can get help faster as people can refer to your proper formatted code and have a link towards lesson for them to refer.

Your last code is not executed because you had removed the curly brackets for the second loop which means the IF statement is not executed inside the second loop, therefore, no true statement is matched and nothing gets printed.

It’s not about the brackets. Getting a tick in CC lesson does not mean that your code is fully functional, it only means that the SCT/validation system is letting you pass. It’s great that you notice even you get a tick, your code is still not functioning as to what is expected.

Your first code is correct syntax-wise. Therefore, the first code is objectively correct compared to second code (which is not functioning towards the objective). However, the lesson might be looking for other things, so a link is ultimately what you need to provide for anyone to help, if you still want to figure out what does the lesson want.

Cheers :slight_smile:


#11

Thank you very much for the reply .
And i will do keep those points in mind . (About formatting the code and all)
And you have very well explained the errorrs in the code .
but i do have a bit of confusion regarding your last para . would u mind explaining that a bit ?
and also would u suggest me to do the loops chapter again maybe ?
Thanks again


#12

Hi @zaidtechnologist,

You meant the last paragraph?

This one?


#13

Hi @faircloud64256, just want to say your initiative and effort to look for the lesson should really be applauded. Really appreciated. (Me think) Don’t be reluctant to ask for link to the poster if you want to help more or just feeling want to contribute more. The community need users like you to keep thriving. :slight_smile:

That said, I just felt that it is supposed to be the poster duty though, to provide the link, which the issue had to be repeated over and over in the community countless of time. All that aside, I believe you both are talking about different exercises from different tracks, thus having different instructions. This case, I think the lesson you’re referring is from the Introduction to JavaScript track, but the poster is referring to Learn JavaScript track.

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

Hope you don’t mind me jumping in this topic to add on that observation. Cheers :wink:


#14

yes . that para itself . im afraid i havent quite understood that.


#16

Hi @zaidtechnologist,

The instruction copied from the lesson (again, I’m assuming the lesson I found since you still do not want to provide any links)

The purpose of the program is to see what you and your friend have in common. Let’s utilize the === comparison with an if/else statement.

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.

So when I said:

I help formatted your first posted code at the very first post:

var myPlaces = ['UAE', 'Singapore', 'Hyderabad'];

var friendPlaces = ['Canada', 'Malaysia', 'Hyderabad'];

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]);
        }
    }
}

So your first code, syntax wise is correct :white_check_mark:and also you achieved the purpose of the program :white_check_mark: (that’s why I said it is objectively correct, the code serve its purpose towards the objective and the goal of the lesson/program intended - matched place printed). Because what the lesson wanted is for you to print your places, then let the loop print your friend places, and print a matched place if there’s any. So you should be curious by now, your code is working, why the lesson not letting you pass, because there’s another reason I will talk later.


Your second code after you experimenting and changing the code, it is getting further away from the goal and the objective, by omitting the brackets, you thought that is correct because the SCT/validation system is giving your a green tick, but in reality, you also notice that why there is no matched being printed. That means your code is not functioning or another word faulty. That is why I said, the second code is not functioning towards the objective of the program.

Basically, I can say your second code is not the answer the lesson wanted. (Please ignore the fact why it is giving your a green checkmark or tick, it is a whole separate matter which involves how CC write the code behind the system to check your answer, I have no idea and power on how they do that.)


So in the end, I suspect what triggered the error for the lesson based on your first code, the first time when you’re not passing is because of this issue:

Copied from the hint:

if (myPlaces[i] === friendPlaces[j]) {
    console.log('Match: ' + myPlaces[i]);
}                  //  ^ See there's a space needed after the word 'Match: '

That is what I suspect, you simply miss out one space in your console.log sentence, you can try again with what I suggested if you’re curious about what I talking about.


Just a side note:

Do not simply remove { } curly brackets because it is what used to enclose the executing codes referred as Code Blocks. You can have bunch of statements inside the curly brackets, without it, the JavaScript will only expect one statement.

So your second code, when you delete the brackets:

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

JavaScript will interpret it this way:
(The If statement is not part of the second loop)

//First loop
for (var i = 0; i < myPlaces.length; i++ ) { console.log(myPlaces[i]);
     
  //Second Loop
  for (var j = 0; j< friendPlaces.length; j++) console.log(friendPlaces[j]);//<== no curly brackets, JavaScript will think that only console.log need to be executed during the loop
  //End of Second Loop

  //Start of If statement
  if (myPlaces[i] === friendPlaces[j]) { //<==Basically, here the friendPlaces[j] is undefined, and therefore no true statement is achieved, and no match will be printed
    console.log('Match '+ myPlaces[i]);
  }

} //End of First Loop

That is why your second code will never print any matched places, you can try put in three exact places for your places and your friend places, still, it will not print any match because of how you write that code.


Hope this long reply is clarifying the stuff that you don’t understand. I hope I’ve made myself clear too.

Happy coding.


#17

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