Iterating over an array


#1

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
for (languages = 0; languages <= languages.length; languages++) {
console.log(languages);
}

the objective is to log each element of the array to the console- and although the button turned green and said congratulations! the elements didn't actually print to the console .. any reasons as to why?


#2

For a start, you must use a different variable for the "for" loop. In your code you redeclare the "languages" variable, that is, you delete the earlier created array. That's how your code is executed:
1. The "languages" variable becomes an array which contains five values.
2. The "languages" variable becomes equal to 0.
3. The computer checks if the "languages" variable is less than or equal to its length. It returns false, because the length property isn't to be used with numbers at all.
4. Since the condition is false, the code inside the "for" statement isn't executed.

Here's the right code:

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
for (i = 0; i < languages.length; i++) {
    console.log(languages[i]);
}

You've made another mistake. As you've noticed yourself, the objective is to log each element of the array to the console. That's why you should have used "languages[I]", not just "languages".


#3

omg! i spent over 45 minutes trying to find out what was wrong with this simple task....because of a semicolon. I have to lol. Watch your semicolons folks!