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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Using Array Indices

Remi Vledder
Remi Vledder
14,144 Points

Second assignment gives a error, however I'm using the same code with a different index

First I use this to get the first value:

console.log(players[0]);

This works just fine and the assignment passes. Then in the second assigment I replace the index to get the last value in the array using this:

console.log(players[5]);

What would be wrong here?

3 Answers

I just did the code challenge twice and it worked for me.

var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];
console.log(players[0]);
console.log(players[players.length-1]);
Remi Vledder
Remi Vledder
14,144 Points

Ah I see, the old one containing the first assignment must stay in place in order for it to work... Thanks! :)

Likely there are not 6 elements in players. If you are looking for the last in the array you could do something like this

console.log(players[players.length - 1]);
Remi Vledder
Remi Vledder
14,144 Points

Thanks for the approach Jason.

However it still doesn't work. I receive the error: "Oops! It looks like Task 1 is no longer passing."

The array in the assignment is this one by the way: var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];

As you can see there are six names in it. Is it a bug?