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 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.