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

Append array within For in Loop

Once you have a value, append it to the results array. This way once the for the loop has iterated over the entire range, the array will contain the first 10 multiples of 6.

I feel like I am over thinking this one. I'm pretty sure I need to put this within the For loop but I'm not 100% sure. I also know that the current results.append(multiplier * 6) wouldn't be correct since it's an Int array.

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

for multiplier in 1...10 {

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

}

4 Answers

Reed Carson
Reed Carson
8,306 Points

You're close, just get rid of the new results variable you made. use the old one with the empty array. The for loop iterates over the given range (1...10) and appends each number * 6 to the array.

Reed Carson
Reed Carson
8,306 Points

yes. after the loop runs, you could imagine the results array now looking like

var results: [Int] = [6,12,18,24,30,36,42,48,54,60]

That did it. So then the results.append(multiplier * 6) was correct.

Just to make sure I understand what you said, by putting the range in the for loop (1...10), I could then leave the array range empty, as the results.append(multiplier * 6) within the For loop would append back to that blank array?

Thanks again

Awesome. That makes sense. Thanks again man