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

Matt Smith
Matt Smith
878 Points

Stuck on first part of Challenge. How do I add the for loop into the var results array?

How do I add the for loop into the var results array?

loops.swift
// Enter your code below
var results: [Int] = [let multiplier = for multiplier in 1...10 {
print(multiplier * 6)
}]
Matt Smith
Matt Smith
878 Points

The aim is a 6 times table in the array.

3 Answers

Lisa Nguyen
Lisa Nguyen
22,731 Points

Something like this should work: var results: [Int] = []

for multiplier in 1...10 { results.append(multiplier * 6) }

So for the multiplier, it first stores 1, multiplies this by 6, appends the result (6) to the results array, and after that it moves onto the next number which is 2 and does the same process again.

Let me know if this helps! :) If not, I can try explaining it better.

Lisa Nguyen
Lisa Nguyen
22,731 Points

Hi Matt, to add each result to the array, use:

results.append(multiplier * 6)

Also, you won't need let multiplier = or the square brackets that surrounds the loop. The constant itself is declared in:

for multiplier in 1...10

Matt Smith
Matt Smith
878 Points

How do I do this using a for loop like the ask requests?