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 trialKevin Coleman
1,123 PointsHow is 6 not the 5th value? If arrays are numbered from 0, shouldnt 6 be the 5th value?
Not sure if im looking at it the wrong way. Objective is asking for the 5th value in the Array.
// Enter your code below
var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts += [8]
let value = arrayOfInts[6]
2 Answers
Steven Deutsch
21,046 PointsHey Kevin Coleman,
You are correct about how indexes are numbered, however, you are passing in the wrong index. Indexes are numbered starting from 0, so to access the 5th item in the array, you need to pass in an index of 4.
// Enter your code below
var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts += [8]
/* You specify the index for what item you want to access
inside of the brackets. */
let value = arrayOfInts[4]
Good Luck!
Kevin Coleman
1,123 PointsThat actually helps a lot! Makes perfect sense now. Thanks! Steven Deutsch
Kevin Coleman
1,123 PointsKevin Coleman
1,123 PointsAhhh i see. So im counting from 0 but starting at "1" being 0, when i should be counting from 0 with "1" being 2. Right? Steven Deutsch
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsTake a look at the following code and see if this helps clear things up.
The first item in the array will always have an index of 0. The last item in the array will always have an index that is one less than the total number of items in the array.