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

Help on Javascript code challenge!

I am completely stuck on the Getting and Setting code challenge in the JavaScript Foundations course. I've tried everything that I can think of. It could be that I'm reading the question wrong since I think it is written strangely. Help?!

"The variable 'thirdElementInArray' was supposed to be the third element in the array. Fix the code so it 'thirdElementInArray' is the actual third element in the array."

<!DOCTYPE html> <html lang="en"> <head> <title> JavaScript Foundations: Arrays</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Arrays: Getting and Setting</h2> <script> var myArray = ["sugar", "rush", "fix", "it", 3.14, 42]; var thirdElementInArray = myArray[3]; </script> </body> </html>

7 Answers

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

EVERYONE - Laurel has the right Answer - var myArray = ["sugar", "rush", "fix", "thirdElementInArray", 3.14, 42]; var thirdElementInArray = myArray[2];

I'm still not understanding this. I get that you start with counting at 0, so in Laurel's example, you'd have:

sugar = 0, rush = 1, fix = 2, thirdElementinArray = 3

So why would "myArray[2]" call "thirdElementinArray"? It seems [2] should actually call "fix"?

I mean, yes, it works, but I do not understand why...?

okay I finally figured it out. You don't have to add the string "thirdElement" into the array. they want you to change the number in myArray[]; so it gets the third element. I had to write it out to finally get it.

[0] is the first element in an array. [1] is the second element in an array, [2] is the third element, [3] is the fourth element.

The index starts at 0, so myArray[3] is actually the fourth element in the array. Ps: You tagged this under PHP.

Thanks, Pedro. Not sure how it ended up under PHP. Meant to tag JavaScript. It's fixed now. :-)

I realize that the index starts at 0, but I guess I'm not understanding what I need to change. Do I change the "it" element in the original code? Or do I write a new line like this to change it- myArray[3] = "thirdElementInArray"

Not sure what I'm missing.

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

The variable is being assigned to the element "it" because myArray[3] corresponds to the 4th position in the array. myArray[0] will be "sugar" myArray[1] will be "rush" and so on...

They want you to retrieve the actual third element which is the string "fix".

Hope it helps.

Confusingly, the first element of array in Javascript is position 0, not position 1 as any reasonable person would expect. That mean for any array, Something[0] is the first element, Something[1] is the second element, Something[2] is the third element and etc. In the quiz, it wants the third element of var thirdElementInArrray. Can you check what position it's setting to now?

Ahhh! I knew it was something with the wording in the question that I wasn't understanding! Thank you very much, Pedro.

I'm still having trouble, and I feel like I understand counting from 0. I've tried a couple different things and keep getting bummer. Do they want thirdElement in array to be in the third spot in the array and then change the myArray to 2, but then the bummer says it expected "fix". So then I put the third elementArray after fix and before it, and it still says no. I'm at a loss. var myArray = ["sugar", "rush", "fix", 'thirdElementInArray', "it", 3.14, 42]; var thirdElementInArray = myArray[3];