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 Swift Collections and Control Flow Control Flow With Loops For In Loops

Having trouble appending to array

I'm still not sure how to append to the array. Any ideas?

loops.swift
// Enter your code below
var results: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for multiplier in 1...10 {

}

3 Answers

Steven Parker
Steven Parker
231,198 Points

First, you don't want to place any values into the array before the loop starts. The code in the loop will fill the array in.

Your loop looks good, so now you could use the "append" method on the array to add a new element, giving it a value six times larger than the "multiplier".

So that would look like this:

var results: [Int] = []
for multiplier in 1...10 { 
  results.append(multiplier * 6)
}

Can you show me what that looks like?

Steven Parker
Steven Parker
231,198 Points

OK, I expanded my answer.

Thanks! I was so close!

Steven Parker
Steven Parker
231,198 Points

Glad to help, "cousin". :wink: And happy coding!