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 trialMarguerite Holden
6,075 PointsThis seems like an easy challenge but not getting it.
Can someone help me here?
grocery_list = ["milk", "eggs", "bread", "ice cream", "potatoes", "pie"]
grocery_list.first[0]
3 Answers
Seth Kroger
56,414 PointsTo reference an item in an array we usually use the brackets after the array: array[position]
We start counting the postition from zero, so the first item in the array is
grocery_list[0]
Ruby does have a .first
methods for arrays and can be used like this:
grocery_list.first
But you don't use both together. Also the challenge says to put the item in a variable called first_item
so don't forget to assign it.
first_item = grocery_list[0]
Luke Pettway
16,593 PointsThere are a few things here that are preventing you from reaching the correct answer:
- You need to create a variable called first_item.
- To access the first item in a standard array you use the arrays variable name followed by [0]. grocery_list[0] for example.
- Assign the first_item variable the first item in grocery_list like below.
first_item = grocery_list[0];
Marguerite Holden
6,075 PointsThanks for your explanation. Very helpful.
Marguerite Holden
6,075 PointsThanks for explaining this. Gives me much clarity.