Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Collections and Control Flow Control Flow With Loops For In Loops

Jeremy Elsmore
Jeremy Elsmore
1,020 Points

I need help on this question about For In Loops in Swift.

I'm not understanding what I'm supposed to do. Do I turn this into a dictionary?

var results: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for multiplier in results {

print("\(multiplier) times 6 is equal to \(multiplier * 6)")

}

This prints out the multipliers. What else is needed? The instructions are confusing.

This is what I thought it was asking for, but it says it's wrong.

results.append(6, 12, 18, 24, 30, 36, 42, 48, 54, 60)

1 Answer

Nathan Tallack
Nathan Tallack
22,159 Points

What they are wanting you to do is to have a for loop populate the results array with the multiple results.

Consider my code example below with comments inline.

var results: [Int] = []  // This is the empty array that we will append to.

for multiplier in 1...10 {  // Here we are using a closed range to make a list of numbers to loop over.
    results.append(multiplier * 6)  // Here we are appending the result of the multiplier to the array.
}

I hope this helps. Feel free to ask more questions if you have them. :)

Jeremy Elsmore
Jeremy Elsmore
1,020 Points

Thanks for the clarification! Makes sense now.