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

Jim Brodhagen
Jim Brodhagen
1,081 Points

Stuck on control flow quiz.

Challenge Task 2 of 2

Inside the body of the loop, we're going to use the multiplier to get the multiple of 6. For example, if the multiplier is 1, then the multiple is 1 times 6, which is equal to 6.

Once you have a value, append it to the results array. This way once the for loop has iterated over the entire range, the array will contain the first 10 multiples of 6.

I'm struggling to grasp how this is supposed to come together. I've re-watched the videos leading up to this 3 times now and I guess it's just not making sense. This is the closest I think I've gotten with it. TIA for the advice!

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

for multiplier in 1...10 {
    var results = "\(multiplier * 6)"
}

2 Answers

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

Couple of things.

First your creating a string 10 times over, but your doing nothing with it. That variable you created within the brackets of the loop, "results", is limited in scope, it disappears after the loop has run. You need to append what work is done within the brackets, each time the loop is run to the array variable named "results".

Second, you are creating a string and assigning it to a variable named results, you really want to create an Int here and append it to your results array. Notice the name I choose for the product of multiplier * 6.

var results: [Int] = []

for multiplier in 1...10 {
    var result = multiplier * 6
    results.append
}
Jim Brodhagen
Jim Brodhagen
1,081 Points

Ahhh this is very enlightening. I had suspected that making a string literal was going to be incorrect due to it not an Int. I had not considered using the append method within the loop but now that I'm looking at it, that makes perfect sense. Many thanks, Chris!

Micah Doulos
Micah Doulos
17,663 Points
for multiplier in 1...10 {
results.append(multiplier * 6)
}

This also works.