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

Yun Yin Ku
Yun Yin Ku
1,493 Points

I don't understand about the instruction of the challenge

I don't understand about what result should I put inside the loop

// Enter your code below
var results: [Int] = []

for multiplier in 1...10 {
  print("times 6 equal to")

1 Answer

Magnus Hållberg
Magnus Hållberg
17,232 Points

You are supposed to append the result of the calculation to the results array. Not knowing what the "essence" of the problem is I will try to explain it all on a very basic level. When you create a for in loop you create a constant that will hold the value for each iteration of the loop, in this case that constant is called "multiplier". The values you should iterate over is in this case 1 to 10, so the first iteration "multiplier" will hold the value 1, next iteration 2 and so on. So to calculate something using a for in loop you simply use the constant (in this case multiplier) the operator (in this case you want to multiply) and then the value you want to calculate (in this case 6). Then you want to append the result to the results array, there are different ways of doing that, the simplest way (I think) is to use the append method. So in this case the only line of code you need to write inside the loop is the following:

results.append(multiplier * 6)

That will do the calculation and append it to the array, all at once for the entire range of the loop. Hope this helps