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

Min Choi
Min Choi
1,517 Points

I don't understand how to append the results of a for in loop.

So for a code challenge there is var results: [Int] = [] for multiplier in 1...10 { print("(multiplier) multiplied by 6 is (multiplier * 6).") }

at this point its asking me to append the values of the entire range into the array but I don't know how to do that.

loops.swift
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
  print("\(multiplier) multiplied by 6 is \(multiplier * 6)")
}
results.append("6")

1 Answer

Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey Min,

you were on the right track but there are a few flaws in your code. In your case you're just printing out something inside of the for in loop and you're appending the string "6" to the results array outside of the loop after the loop was already executed. Firstly to solve the challenge you can remove your print statement inside of the loop.

Then you can put your append method on the results array inside of the loop and instead of appending the string "6" just append the multiples of 6 to the results array by multiplying 6 with multiplier.

Like so:

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

I hope that helps, if you have further questions feel free to ask! :)

Min Choi
Min Choi
1,517 Points

Thanks for the help. I figured out that I wasn't supposed to get a string so I got multiplier * 6 but i didnt figure out how to append it. Thanks for clearing that part up.