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

For In Loops

Hi guys, I am having a hard time on this code challenge. Can someone post the code in the comments section? Thanks.

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

for multiplier in 1...10 {
  print("\(multiplier) times 6 equals \(multiplier * 6)")
}

1 Answer

andren
andren
28,558 Points

The problem with your code is that it's doing something different from what the task is actually asking, the task asks you to append the result of "multiplier * 6" to the results array within the loop. It does not ask you to print out any message or anything like that.

The easiest way of appending data to an array is to use the .append method which is found on arrays. Like this:

var example: [Int] = []

example.append(5) // You can type a pure value
// example now holds: [5]
example.append(10 * 2) // You can also type equations
// example now holds: [5, 20]

With the above knowledge in mind it should be relatively easy to solve this challenge, just change your print statement to one that appends "multiplier * 6" to the results array.

If you are still struggling with solving the challenge then I have posted the solution for you here, but it's always best to try to write the solution on your own first.

T hanks andren! Now I know how to do the question.