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

Surendra Kulkarni
Surendra Kulkarni
6,826 Points

setting Array length

In the following code : var myArray = ["sugar", "rush", "fix", "it", 3.14, 42]; var thirdElementInArray = myArray[2]; var myArray = new Array(30); var treehouse = myArray[30]; I have tried to declare a new Array at the beginning but it still does not like it?

4 Answers

Tommy Gebru
Tommy Gebru
30,164 Points

try to make the code more presentable before and after the block of code on separate lines apply this key 3x ( ` ) . Refer to the Markdown cheatsheet on the bottom-right side of the post text-area.

Gareth Borcherds
Gareth Borcherds
9,372 Points

With array's in JavaScript, you can get an element by using the index number of the element. You got that correct in the first task by changing myArray[3] to myArray[2];

In the second task it is asking you to set the 31st element to treehouse. Well accessing that element we would use myArray[30]. We can also set a value for a particular index by using myArray[30] = 'treehouse';

So your final result would look like:

var myArray = ["sugar", "rush", "fix", "it", 3.14, 42];
var thirdElementInArray = myArray[2];
myArray[30] = 'treehouse';
Surendra Kulkarni
Surendra Kulkarni
6,826 Points

I dont agree with that. Here is Oxford dictionary Definition of simultaneously in English: simultaneously Line breaks: simยฆul|tanยฆeousยฆly Pronunciation: /?s?ml?te?n??sli / ADVERB

At the same time: the telethon was broadcast simultaneously on 31 US networks it works simultaneously as character study, teen drama and sci-fi thriller

It is very important for flow of the code, particularly in the context of algorithm.

For clarity the task for the challenge is: "On about line 18, set the 31st element in 'myArray' to the word 'treehouse'.

myArray is already set on line 16, so if you declare it again (by putting 'var' in front of it) it wipes out the existing values.

The correct line 18 is (as Gareth posted above):

myArray[30] = 'treehouse';

This finds index 30 (the 31 item in the array) and sets it to the string 'treehouse'.