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

s t
s t
2,699 Points

Swift 3 Collections and Control Task 2

Here is my code so far for task 2, I just don't know what goes in var results? I have tried all sorts but I don't know how to complete this task. Please provide answer and explain. Thanks!

var results: [Int] = [] for multiplier in 1...10 { ("(multiplier) times 6 is equal to (multiplier * 6)") }

loops.swift
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
("\(multiplier) times 6 is equal to \(multiplier * 6)")
}

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Sharmila,

You've got the right idea! The challenge is asking you to populate the array results with a bunch of numbers that represent the result of multiplying 6 by each number from 1-10. So your array should end up looking like this:

[6,12,18,24... etc.

So to do that, we're going to calculate and append the number each time through the loop:

// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
    results.append(multiplier * 6)
}

Note there's nothing to do with string interpolation - we're just populating the array results.

Cheers :beers:

-Greg

s t
s t
2,699 Points

just not getting it...explanation please!!! I think I get the concept but syntax is wrong

var results: [Int] = []
results.append(6,12,18,24,30,36,42,48,52,58)

for multiplier in 1...10 {
("\(multiplier) times 6 is equal to \(multiplier * 6)")
}
Greg Kaleka
Greg Kaleka
39,021 Points

Hah - you can't manually append all the values! That's cheating :wink:. Imagine you had a thousand values to append. Also, you made a math error, which is another reason not to do it manually.

You gave it a solid try, so I've updated my answer - take a look. Make sure you really understand what's happening and why. Ask any questions you have.

s t
s t
2,699 Points

ok, makes sense now. I also see it in the developer guide too. Thank you!