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 trialArthur Kakulidis
2,657 PointsWhat am I doing wrong?
I don't know how my code is wrong.
var snacks = ["chips", "popcorn", "crackers"]
console.log(snacks[0]);
alert(snacks[4]);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Arrays</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
4 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHi Arthur,
You're doing great.
But the 3rd task says to alert()
the third item. Right now, your code is trying to alert()
the 5th item, which doesn't exist. Remember, arrays use zero indexing, so the 3rd item with be at index 2
.
Keep it up! :)
Natalie Tan
25,519 PointsJavascript array starts with 0 index.
For your array
var snacks = ["chips", "popcorn", "crackers"]
- chips is at 0 index and is accessed by snacks[0]
- popcorn is at 1 index and is accessed by snacks[1]
- crackers is at 1 index and is accessed by snacks[2]
snacks[4] does not exist in your array
Brandon Evanson
9,482 PointsIf you count the snack list starting at 0, the third snack would be 2
Arthur Kakulidis
2,657 PointsOh! I'm stupid... Thanks guys!
Jason Anders
Treehouse Moderator 145,860 PointsNah, never "stupid". And don't worry... we all have and still do make silly mistakes every now and again. It keeps us on our toes. ?♂️