Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Arrays Getting and Setting

31st element, 30th position (Array) lingo

Hello!

The task is to set the 31st element to 'treehouse'. I am able to pass it through trials and error, but the lingo is throwig me off. I know that arrays are zero based.

1) Should I look at the 1st element to mean the length up to that array element, but the real position of the value in the 1st element to be, myArray[0].

likewise should I look at '31st element' to mean the length of the array up to that point is '31', but the position for the last element up to 31st element to be myarray[30]

Ok, this is might be a twisted way to explain to word and number used for the length of an array, and the word and the number used to explain the posistion of an element. If someone could help me with the lingo, I would really appreciate it. It could help me with future task. Cheers!

<!DOCTYPE html> <html lang="en"> <head> <title> JavaScript Foundations: Arrays</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Arrays: Getting and Setting</h2> <script> var myArray = ["sugar", "rush", "fix", "it", 3.14, 42]; var thirdElementInArray = myArray[2]; myArray[30] ="treehouse";

</script>

</body> </html>

3 Answers

Quote from Accessing Array Elements

"JavaScript arrays are zero-indexed: the first element of an array is at index 0, and the last element is at the index equal to the value of the array's length property minus 1."

So an array with a length of 31 would have indices that go from 0 to 30 but the element numbers would go from 1 to 31. The element number is 1 more than the index number.

It's just terminology. Array indices are zero-based but when talking about the element numbers there isn't such a thing as the 0th , or zeroth element of the array. The element numbering starts at 1.

So when given the task of setting the 31st element you should reason out that the actual index number is 1 less than that. The index would be 30.

Andrew Boryk
Andrew Boryk
15,916 Points

Need a little clarification on the question. Do you mean that you want another reason as to why and how to find the element you want? I basically look at it as, the array starts from zero because zero is the first non-negative number. Then for every element after the first one, you just take the element you wish to find, and subtract one from that number.

The length of the array if it had 31 elements, would be 31. The length is the number of elements in the array. The index is basically is the spot of the array that the element is in. So, the 31st element is the last element, it is located at the 30th index, and shows that the array is 31 elements long (length of 31).

Does that answer your question?

Ok, when I hear ' #elements' of an array, I should automatically assume they are talking about the length of the array. The position will always be expressed as 'index', and because arrays start from zero, like you clearly explained , the last position of an array of 31 elements is '30th index'. Thank you Andrew!

Thank you Jason! I good on the lingo now!!!