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

Stuck in a code challenge...

Hi guys!

I'm currently stuck on this code challenge: https://teamtreehouse.com/library/swift-3-collections-and-control-flow/control-flow-with-loops/for-in-loops

I am on the second task. I honestly don't know how to solve this. However, I managed to solve task 1 and this is what I came up with:

for multiplier in 1...10 { print (multiplier) }

I know that task 1 and 2 are connected. I need your assistance in helping me understand how to solve task 2.

Anyone's help is very much appreciated! Thanks a lot!

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

Ok, so we have an array, that has no values in it, and we want to add the result of a multiplication in it. We want to use the for in loop.

So far you are doing well. Remember a for in loop goes through the range one by one, so what do you want to do with the first number? In this case 1? Do you want to multiply it by 6 right?. Well after that the loop will continue through all the numbers until it gets to number 10 and stops the execution.

for multiplier in 1...10 {
 // Multiply the "multiplier" by 6
// then add it to the array.
}

I did not give you the full answer, this way you can figure it out on your own. Good luck

Hint: Arrays have a method called "append()"

Hi Jhoan!

Thanks for your response. Unfortunately, I still don't get it.

I came up with this code: print (" \ (multiplier) times 6 is equal to \ (multiplier * 6)") Which, I believe, gives me the multiplier of 6 from 1 to 10.

But what do I do next? Am I supposed to use the code given above: var results: [Int] = []

Jhoan Arango
Jhoan Arango
14,575 Points

Hey, Christian, that's ok. It's not easy to get this at first.

Here :

var results: [Int] = []

for multiplier in 1...10 {
    let result = multiplier * 6 // We multiply and add it to a constant
    results.append(result) // Constant "result" is appended to the array
}

// or 

var results: [Int] = []

for multiplier in 1...10 {
    results.append(multiplier * 6) // We multiply directly in the "append()" function.
}

// or 

var results: [Int] = []

for multiplier in 1...10 {
    results += [multiplier * 6] // We multiply it inside an array and add it to the results array. 
                                // This is called appending an array with the addition assignment operator.
}

You can try any of these approaches.

By the way, deleted the "print" statement, if you want to pass the challenge.

Good luck, and happy coding.