Can anyone explain this code


#1

Why did this work? I am actually not sure :disappointed:

Can anyone explain this.

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];

for(var languages = 0; languages < 5; languages++){
    console.log(languages);
    }

#2

we have two things here: languages, which is an array

then we have a for loop, in which we are going to use a counter, but you give the counter the same name as the array, are you certain you want to do this?

whatever you call your counter, you should use your counter as index to retrieve items from the array:

arrayName[index]

can you provide the exercise url?

If you need more help, also post an updated version of your code please


#3

https://www.codecademy.com/en/courses/javascript-beginner-en-9Sgpi/0/4?curriculum_id=506324b3a7dffd00020bf661#

Yes the hints say the answer should be this but I still got my top one right. Is there any differences. This is what code academy wanted.

for (var i = 0; i < languages.length; i++) {
    // console.log the 'i'th element
    // of the languages array
}

#4

well, given your array and the counter the same name is a bad idea.

you hard-coded 5 in the loop condition, what if the array changes size? then length would be a better choice (its better to learn good habits early)

your code only output numbers ,not elements from your array, is this the desired result?


#5

Oh yeaahhhh that's actually a good idea instead of typing how many elements are thanks for the support


#6

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