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
16,953 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