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 trialAnton Shelepov
13,030 PointsCode 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]);
var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];
console.log(players[0]);
var num = players.length - 1;
console.log(players[num]);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Joe Beltramo
Courses Plus Student 22,191 PointsThe 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
13,030 PointsAnton Shelepov
13,030 PointsAwesome! Thank you! I was worried that passing ".length" when working with arrays was not allowed by the JS which made no sense.