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

I dont understand part 2 of the For In Loops challenge.

It says "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 the 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 did var results: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let multiplier = results for multiplier in results {"(multiplier) times 6 is equal to (multiplier * 6)"}

what am I missing?

loops.swift
// Enter your code below
var results: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let multiplier = results
for multiplier in results {"\(multiplier) times 6 is equal to \(multiplier * 6)"}
var loop = [12, 18, 24, 30, 36, 42, 48, 54, 60]
results.append (loop)

5 Answers

I'm going to try to simplify what you were doing by first going back to the first question. It asks:

  • Create a for in loop that iterates over a range from 1 to 10. Name the temporary constant "multiplier".
for multiplier in 1...10 {
}
  • Use multiplication to get the multiples of 6 from the loop
for multiplier in 1...10 {
multiplier * 6
}
  • Append it to the given array "results"
for multiplier in 1...10 {
results.append(multiplier * 6)
}

I suppose if you wanted to create a variable for "multiplier * 6" you could, however, I'm not sure Treehouse would accept it as an answer. In any case, it does work with this local variable as well but it's more code. If you were going to use multiplier * 6 again, then it fits the DRY principle (don't repeat yourself).

var results: [Int] = []
for multiplier in 1...10 {
    var multiplicationOfSix = multiplier * 6
    results.append(multiplicationOfSix)
}
Jakob Wisener
Jakob Wisener
2,860 Points

I am stuck on the same area. I have tried

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

yet I keep getting an error of "Your array needs to contain the first 10 multiples Make sure your range goes from 1 to 10". I guess I am just not understanding this part of the challenge. Any clues to what I am missing?

Trent Porter
Trent Porter
611 Points

I'm stuck on this as well. The question doesn't really make sense because I feel like I already did that before. I changed the code around multiple times and it still isn't working.

var results: [Int] = [
    1,2,3,4,5,6,7,8,9,10
]

for multiplier in results{
    print("\(multiplier * 6)")
}

This is how I started the first section and passed it, now I have tried a few different routes. It says we need to change the loop to get the multiple of 6. This is what I had done already. After about 45 minutes of struggling through, I figured it out.

What you do is take the values you originally put in the Array and delete them. The first portion of my code I had set numbers in the array multiplied by 6. In this section, you need to take an empty Array and use the for in loop to update the values in the Array. So essentially you do the exact same thing, but instead of defining the numbers in the Array, you're defining the numbers within the loop.

The question definitely need MORE information to it, because that was 90% of the issue for me. Lack of wording or examples left more to be desired.

var results: [Int] = [
]

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

this worked for me... thanks!!

Thank you for explaining that. This was a head scratcher for me.

var results: [Int] = []

for number in 1...10{

    /* declare a constant name multiplier 
           Then multiplier 1 x 6, 2 x 6, 3 x 6 ...... 10 x 6
              The primary digit is coming from our  (For in loop)
                  It's looping from 1 to 10 */

                     let multiplier = number * 6

    /* Append the result from multiplier and storage it on our array variable (results) */
           results.append(multiplier)
}
Ryan Rassoli
Ryan Rassoli
3,365 Points

I put this and im still given a compiler error.

this is what I put...

// Enter your code below var results: [Int] = [1,2,3,4,5,6,7,8,9,10] for multiplier in 1...10 { print ("(multiplier) times 6 is equal to (multiplier * 6)") } let multiplier = number * 6 results.append(multiplier)

var results: [Int] = [] for multiplier in 1...10 { var multiplicationOfSix = multiplier * 6 results.append(multiplicationOfSix) }