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

Brian CI
Brian CI
28,971 Points

I don't understand the assignment

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.

To me that says to do this:

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

Doesn't make much sense becaues there is no value given for what should be thirdElementInArray. Also the error message "Remember array index always starts with zero", makes no sense. What does that have to do with anything?

5 Answers

Array indexes begin with 0 is the most important part of the question. myArray indexes should be listed as:

myArray[0] = "sugar";
myArray[1] = "rush";
myArray[2] = thirdElementInArray

They want you to edit the line that reads: var thirdElementInArray = myArray[3] so that thirdElementArray is in the third spot in the array and not the fourth.

Brian CI
Brian CI
28,971 Points

Yea but that doesn't work, that's what I tried.

What works is to assign the value of the third element in the array to thirdElementInArray.

I think what they should have said is "Assign the value of the the third element in the array to thirdElementInArray" instead of "The variable 'thirdElementInArray' was supposed to be the third element in the array." which to me sounds like either the code you provided or I provided but not what is the correct answer.

The correct answer was to just change the line to :

thirdElementInArray = myArray[2]

instead of

thirdElementInArray = myArray[3]

Brian CI
Brian CI
28,971 Points

or not... after successfully passing task 1 with this and then without changing it and moving on to task 2 I now get this:

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

<!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[2];
      var myArray[30] = "treehouse";
    </script>
  </body>
</html>
Brian CI
Brian CI
28,971 Points

Oops I meant to post the error message:

Bummer! Was expecting 'thirdElementInArray' to be "fix" not "undefined"

christopher couret
christopher couret
10,297 Points

introduce the thirdElmentInArray as the 2 array because it starts from 0