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

Yan Malyakov
Yan Malyakov
2,371 Points

Now, use console.log() to log out the last item in the array.

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

i understand it is simple question but i try write answer //1) console.log(5); //2) var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick']; var lastItem = players[5] ; console.log(lastItem); //3) console.log(players.length - 1); please help it doesn't work

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

5 Answers

Adam Beer
Adam Beer
11,314 Points

Simple. Please delete this 2 lines. You dont need it.

var lastItem = players[5] ; 
console.log(lastItem);

Use only 2 console.log. Inside the first console.log log the first element and inside the second console.log log the fifth element. I hope this help!

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

Enjoy the coding!

Adam Beer
Adam Beer
11,314 Points

Hi Yan! When you log the first item in array give the index of property. Like this: console.log(players[0]); I Hope this help!

Adam Beer
Adam Beer
11,314 Points

The fifth element was saved in var lastItem. After inside the console.log log the fifth element in array. Use player(this is the variable name) in the console.log and add index of property.

Yan Malyakov
Yan Malyakov
2,371 Points

Adam,hello i write
console.log(players[5]); it answer me Oops! It looks like Task 1 is no longer passing. if i write console.log(players[5]); it answer me Bummer! Use [ ] and a number to access an item in an array: players[0] where is the problem..please answer me.

Adam Beer
Adam Beer
11,314 Points

Hihi. Tricky task. Dont use second time the first console.log, write new console.log to fifth element. Good coding! :)

Yan Malyakov
Yan Malyakov
2,371 Points

var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick']; var lastItem = players[5] ; console.log(lastItem); //it does'nt work //or var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick']; console.log(players[5]); //it does'nt work //or var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick']; console.log(players.leth - 1); //it does'nt work //please what I write wrong ?