Loops and Arrays I - help to understand


#1


https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/2/3?curriculum_id=506324b3a7dffd00020bf661#

I have no error Messages.My code is correct, but can please somebody explain to me why you have to use "i < cities.length; in "for (var i = 0; i < cities.length; i++) {"? I do not know why you have to use it. I do not understand how the for Loop works with this particular example. please help

var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "Rommerskirchen", "Köln","Dormagen","Stürzelberg","Nievenheim","Grevenbroich"];

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


#2

Is there anything special about this example or are you just asking how a for loop works?

You can read about the for-statement here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for


#3

yes only about this Special example . how works the" i < cities.length;" part in the middle?
I do not understand how it is based on the Loop. Is the length of the word of City meant? or is it concerned to the number of elements in the Array? and when do the Loop stops then?


#4

So you're not asking about a loop at all, but about what cities.length does?
If you look at your code, then you'll see that you have assigned an array to that variable, and an array's length property returns how many elements it has

var myArr = [1, 2, 3];
console.log(myArr.length);  // 3

#5

ah it only tells how much elements are in an Array? So if there are e.g. 9 elements, the program knows to stop after These 9 are listet?


#6

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