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 trialPaolo Pacheco
978 PointsWe also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign
I am trying this
let read: [Int] = arrayOfInts[5] and it does not work
// Enter your code below
var arrayOfInts: [Int] = [2,3,24,5,4,12]
arrayOfInts.append(100)
arrayOfInts + [2]
arrayOfInts[5]
let read: [Int] = arrayOfints
2 Answers
andren
28,558 PointsThere are a number of issues with your code:
The constant has to be named
value
notread
, these challenges are very picky about the names of constants having to be exactly what they instruct them to be.There is a typo in the name of the array variable it is supposed to be
arrayOfInts
notarrayOfints
(you forgot to capitalize the I)The type of the new constant should be
Int
not[Int]
, since you are retrieving a singleInt
from thearrayOfInts
array and assigning it to it.Possible the most important issue, as the instructions remind you array indexes start at 0, this means that to access the first item of an array you would use [0], to access the second item you would use [1] and so on. Because of this you are actually accessing the sixth item by using [5]. To access the fifth item you have to use [4].
You have to remove the dangling
arrayOfInts[5]
that is on its own line not doing anything.While not technically an error it's worth noting that the line
arrayOfInts + [2]
is not actually doing anything useful. This operation returns a new array that contains the content ofarrayOfInts + 2
but it does not actually modifyarrayOfInts
. In order to use the returned value you would have to assign it to a variable. If you actually wanted to modify the arrayOfInts variable directly you have to use the+=
operator instead like this:arrayOfInts += [2]
Edit: Added some points I only noticed after posting.
Paolo Pacheco
978 PointsYep Thanks for the answer, i did not read correctly the instructions :/