Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Loops For In Loops

Rikki Ringgold
Rikki Ringgold
11,565 Points

How do i append the results of a loop to the results of the array? Need to have the array contain the first 10 multiples

Below is my code thus far. But how do I pass the results of the multiplier into the array? Do i just enter it manually? var results: [Int] = []

for multiplier in 1...10 { print("(multiplier) * 6 = (multiplier * 6)

2 Answers

Mike Hickman
Mike Hickman
19,817 Points

Hey Rikki,

I just walked someone through this, so I'm going to just paste that over here.

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).

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

Rikki Ringgold
Rikki Ringgold
11,565 Points

Thanks Mike for the great explanation! I am very new to all of this and am very thankful for this community. I'm sure with practice and repetition I'll learn the concepts eventually.