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

Array Challenge

Does anybody know what they want in this Array Challenge:

On about line 18, set the 31st element in 'myArray' to the word 'treehouse'.

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

2 Answers

Stone Preston
Stone Preston
42,016 Points

the task states: set the 31st element in 'myArray' to the word 'treehouse'

so you need to assign the 31st element in the array the value of "treehouse". you dont need to create a new variable, just assign a value of the 31st item in the array myArray.

you can assign values to specific elements in arrays like so:

some_array[x] = value;

where some_array is the name of your array, x is the index of the array, and value is the value you want assigned to that index. so to set the 5th item of an array some_array to "apples" I would use:

//indexes start at 0 so the fifth element in the array has an index of 4.
some_array[4] = "apples";

remember that array indexes start at 0, so the 31st element is the 30th index.

Thanks Stone,

that was the ticket, i like the way you explain things.