11/13 Loops and Arrays 1, how to reverse the loop


#1

Here is the default code, can anyone please tell me how to loop through the array in reverse order so that it prints out the names of cities starting from 'NYC'?

var cities = ["Melbourne", "Amman", "Helsinki", "NYC"];

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

#2

Thats preety simmilar to the code you showed us... start from the length-1/3, then go to the 0's index :wink:

var cities = ["Melbourne", "Amman", "Helsinki", "NYC"];

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

BTW I updated code i was wrong before.. sorry :frowning:

Here is the compiler showing this code works..


#3

many thanks it is working properly now!