Part 11/13


I am already actually finished with this Unit but i decided to do it again because there was still something that wasn't clear to me. In the part 11/13 my code looks like this:

var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "Riyadh", "Shanghai", "Mumbai", "Virginia"];

for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);

I did exactly as it was instructed and it was correct but my question is about the last part that I did not understand.

console.log("I would like to visit " + cities[i]);

What does 'cities[i]' means here?


Alright, i is your count :slightly_smiling: .

for(var i = 0; i < cities.length;i++)

in this piece of code you want i to count up the the length of the cities array.
in more detail: Each loop of the for loop will add 1 to i: "i++" until the conditions are not met.

now this is useful because to request an element of an array is as easy as


This would log "Melbourne" to the console.

i is simply a placeholder for a series of numbers you want to count, i could be anything.



is literally saying
loop1 cities[0] (console logs "Melbourne)
loop2 cities[1] (console logs "Amman")

I hope that makes sense :sunny:


Very well explained, edwin! Thank you very much!

So if we have to create another for loops do we have to use a different placeholder each time or it doesn't matter?


Well, it makes it less confusing if you use a different placeholder :slightly_smiling: . I'm not sure if it would affect the program using the same i for each for loop; however, it does affect readability in my opinion.

You would definitely use a different place holder for a nested for loop :wink: .

for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);
  for (var x = 0; x < cities[i].length; x++) {
    console.log("I would like to visit " + cities[i][x]);


Alright, got it!

Thank you :slightly_smiling:


Glad I could help :slightly_smiling:


Would you be able to help me with this, edwin?

Even though I have finished this Unit and got this correct but I'm not sure if it IS correct :confused: I still haven't quiet understand from nested for loops (j) till end.

Thank you very much!!!


This is an interesting solution, and it does work! Yet, if there is a "Steve" in your text variable string you might have a problem :slightly_smiling: .

Also, you are pushing letters into an array and checking if name is in string from there. If Sul was in the string and not Sulaiman it would still say it found my name.

Here is what I would do

var text = "Hey Sulaiman! Steve It's been a\
long time. It was nice seeing you,\

function matchInText(word){
    var regex = new RegExp(word, "g");
    var matches = text.match(regex);
    if(matches !== null){
        return matches.length + " Intances of your name was found!";
        return "Your name was not found!";

I probably wouldn't use a nested for loop because it can get kind of messy :slightly_smiling: .

When you use a nested for loop you are usually trying to access something inside another thing.

Like say you have this array.

var myArray = [[1,2,3,4],[5,6,7,8],["dog","john","bill"]]

as you can see, this is a nested array which you will need a nested for loop to access it's contents.

for(var i = 0; i < myArray.length; i++){

With this loop you are logging to the console


Now this nested for loop

for(var i = 0; i < myArray.length; i++){
  for(var x = 0; x < myArray[i].length;x++){

We are going into [1,2,3,4] and each sub-array and grabbing each of it's values :slightly_smiling: .

this loop will print out


I hope that makes sense :sunny:

And if you want me to explain my solution to this problem I would be happy to explain.


Thank you so much edwin for explaining it! :slightly_smiling:

What I had done in my code is basically "followed" the instructions. I still am not sure what exactly "pushing" a letter means and what's "hits" used for :frowning:

I would love to learn your solution! I am totally new to programming and haven't gone beyond "for loops" yet, so you might want to explain it like you're explaining a little kid :wink: haha

Thank you once again edwin. I very much appreciated all your responses, it was very helpful!