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 trialSurendra Kulkarni
6,826 Pointssetting 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
30,164 Pointstry 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
9,372 PointsWith 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
6,826 PointsI 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.
Steven Lacks
18,214 PointsFor 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'.