Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using Array Indices

Does Challenge Task 2 in JavaScript Loops, Arrays and Objects have a mistake in the software?

The question reads as follows: Use console.log() to log out the last item in the array. var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];

Here's my answer: console.log(players[5]);

I've tried all numbers from one through six and nothing works.

script.js
var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];
console.log (players[6]);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

andren
andren
28,503 Points

The first index you tried (5) is indeed the correct one. The issue is that you have removed / changed the code you wrote in task 1. Below the challenge description of every task the following text is written:

Important: In each task of this code challenge, the code you write should be added to the code from the previous task.

What that is trying to tell you is that for multi-part challenges you should not remove previous parts, but add to them. When you try to check your code the challenge checker will run all of the tests from all of the tasks it has given you so far, and if your code does not pass all of them (not just the test for the current task) then it will be marked invalid.

So if you add the code for task 1 back like this:

var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];
console.log(players[0]);
console.log(players[5]);

Then your code will pass.

Thanks