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

Joey C
Joey C
1,837 Points

Could someone please explain what I did wrong here?

I am doing just as the previous video states, but I get this message: Bummer! Make sure the results array contains the first 10 multiples of 6

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

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

1 Answer

Mike Hickman
Mike Hickman
19,817 Points

Hi Joey,

So, the basic idea is now that you've created the empty for loop in step 1, you're going to append a number to the results array each time the loop runs (as it goes 1 - 10). Right now, you're printing, which is doesn't ask you to do.

Let's start fresh at the beginning of the step 2 challenge:

var results: [Int] = []

K. We have an empty results array. Perfect.

for multiplier in 1...10 {

}

Just in case you're newer to loops, the way ours is setup is saying "every time we run through the loop, whatever number in the range (1-10) we're on, that number is called "multiplier".

Cool. We're back to the empty for loop that you created in step 1. Now, literally the only thing it wants you to do is append a number with each run of the for loop into the results array. Think of it as just adding a number into a list in the array. Example: results = [1, 2, 3, 4]. Luckily, we can use the .append method to do this.

for multiplier in 1...10 {
 // grab the results array and append(add) something
 results.append()
}

We want to append 6 * whatever the current number ("multiplier") is as we do the loop.

for multiplier in 1...10 {
 // let's throw the actual multiplication in now to finish it off
 results.append(multiplier * 6)
}

So, when we start the loop, it'll be results.append(1 * 6), then results.append(2 * 6) in the next loop. Each time the loop runs we're adding a number to the list in the results array. Once your array is setup in that initial first line, you never have to touch that line of code again for this exercise. All of the numbers are pushed into the array from within the for loop.

Hopefully I didn't OVER explain it..just wanted to help you understand in case you're new to all of this.

Another thing to think of as you progress. You don't ever have to print or return anything unless it specifically tells you to do so.

Have fun!

Mike