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

The variable 'thirdElementInArray' was supposed to be the third element in the array. -JavaScript Foundations

This question pertains to Task One of the Creating Arrays Code Challenge.

I've tried a couple different approaches, none of which are correct.

This is what I've tried:

Ken Alger
Ken Alger
Treehouse Teacher

Ashley;

You code didn't show up. Would you mind posting it again?

Thanks,

Ken

2 Answers

geoffrey
geoffrey
28,736 Points

You just have to change the index inside the brackets.

var myArray = ["sugar", "rush", "fix", "it", 3.14, 42];
      var thirdElementInArray = myArray[2];

Type 2 inside [ ] next to myArray, because, as It's asked you want the variable thirdElementArray to be the third one of the array myArray. As we are in an array, the index is 0 based. So we start counting at 0, that's why If we want the variable thirdElementInarray to be the third, we set the value of 2 inside the brackets.

If you want for exemple to set the value of another variable to sugar you would typically do this.

var newVariable =  myArray[0];

To experiment with this quickly, you can open your console and play with it. You'll quickly see the way it works.

thank you, geoffrey

I counted the zero and was using [4] as the value. Thanks for your help and explanation.