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 code Code Challenge isn't working

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

Here's my answer:

var myArray[31] = "treehouse";

for some reason the problem wont go through, it keeps saying now I have problem 1 wrong, which does not make any sense. Can someone please help?

8 Answers

the 31st element is actually the 30th index since arrays indexes start at 0, so setting myArray[31] is incorrect because it isnt setting the 31st element, its setting the 32nd

Still no luck... var myArray = ["sugar", "rush", "fix", "it", 3.14, 42]; var thirdElementInArray = myArray[2]; var myArray[30] = "treehouse";

oh its because you have var in front of myArray[30]. remove that so you should have:

 myArray[30] = "treehouse";

if you put var in front it defines a new myArray variable, which means task 1 no longer passes since it doesnt hold the same value

Taking away the var worked for me, Stone Preston, but I'm still not sure why that worked. Could you explain why keeping the var makes task 1 not pass?

I am having an issue as well. I typed out my code using the 30 value instead of 31 but I am recieving the error that Task 1 is no longer passing. Any advice for what may be causing the problem?

//about about getting rid of the var should look like this: myArray[30] = "treehouse"; it worked for me

That worked! Thank you. Makes sense too, there's no need to define a variable there.

Its not working for me either. It says that the first one is no longer working now. I tried getting rid of the "var" in the third line but still doesnt work.

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

nevermind was using the wrong kind of brackets () vs. []

I too am getting the response Task one is no longer passing. with this answer ...

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

I also tried... var myArray[30] = "treehouse"

it could be your use of single quotes. try using double quotes

var myArray[30] = "treehouse";

Thanks, that fixed it! Yeah!

Hey everyone,

It seems like we are not suppose to define a VARIABLE because it is already there! We only need to define a VALUE for the 31st element in the array:

myArray[30] = 'treehouse';