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 trial

iOS

Matti Helenius
Matti Helenius
421 Points

Reading an item from array into a variable and assign the result to a constant named value. Can't understand what to do.

Hi,

I'm doing this exercise but im not sure how to do it. Link to the exercise; https://teamtreehouse.com/library/swift-3-collections-and-control-flow/introduction-to-collections/working-with-arrays (3/4).

"We also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign the result to a constant named value."

Heres my code, I tried to comment it aswell. The last 2 lines are the ones im trying to save value into a variable but dont know how to pass this one. Help Guys

// Mission 1
// Making an array of 5 - values, 0 to 5
var arrayOfInts: [Int] = [0,1,2,3,4,5]

// Mission 2
// adding 1 more value with an append
arrayOfInts.append(6)

// adding 1 more value with a other style
arrayOfInts = arrayOfInts + [7]

//Mission 3
// saving arrays 5th items into a variable
let eiOleValia = arrayOfInts[4]

//print out the variable
eiOleValia

Thanks in advance!

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

So the challenge actually ask you to assign the fifth element in the array to a constant called value. In all the other challenges they put variable and constant names in a different font so their name are a little bit more clear. I guess they forgot to do that for this challenge. Here is what you code should look like:

// Mission 1
// Making an array of 5 - values, 0 to 5
var arrayOfInts: [Int] = [0,1,2,3,4,5]

// Mission 2
// adding 1 more value with an append
arrayOfInts.append(6)

// adding 1 more value with a other style
arrayOfInts = arrayOfInts + [7]

//Mission 3
// saving arrays 5th items into a variable
let value = arrayOfInts[4]
Matti Helenius
Matti Helenius
421 Points

Thank you so much sir, I knew I was close. :)