iOS Swift Collections and Control Flow Control Flow With Loops For In Loops

Having trouble with appending array

What is wrong with my code? Any ideas?

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)")
}

1 Answer

Jhoan Arango
MOD
Jhoan Arango
Treehouse Moderator 12,798 Points

Hey There :

So, you are close. Few things you need to change. The results array needs to be empty when you first declare it. Then in the for in loop, you will add values to it as you iterate. Also, in the body of the loop, the challenge is not asking to do a print, but to append the value of the result into the "result" array.

Here take a look at this:

// Empty array
var results: [Int] = []

for multiplier in 1...10 {
  let result = multiplier * 6

// Append the result into the array
  results.append(result)
}

Hope this helps you