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

Anton Shelepov
Anton Shelepov
13,030 Points

Code works perfectly fine in my browser but shows wrong answer here.

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

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

1 Answer

The compiler on Treehouse is being picky and would like you to use an actual number. You are correctly accessing the array though. Either move the assignment for num into the brackets or just use [5]:

i.e.

console.log(players[players.length - 1])

// or

console.log(players[5])
Anton Shelepov
Anton Shelepov
13,030 Points

Awesome! Thank you! I was worried that passing ".length" when working with arrays was not allowed by the JS which made no sense.