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

Do not completely understand the question

The following is part 2 of my assignment. please, correct me: I think I have to read the "for loop" using the array, but not sure how to execute this.

Please, advise

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

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

7 Answers

Hey Michael,

You've got the right idea. You don't need to print anything to the screen. Instead, all you need to do is add the result of multiplier * 6 to the array results each time through. That will append 6, 12, 18, etc. to the array.

To append to an array, you simply write someVariableArray.append(someValue). In this specific case you should write results.append(multiplier * 6).

Let me know if that makes sense.

Cheers :beers:

-Greg

Thanks Greg, this is helpful!

I have to get used to how they formulate the questions, which causes me to overthink at times. But, yes! This makes sense.

Q: any idea why Treehouse displayed the first line of code? The second line of code is mine.

The first line of code is important! The whole purpose of the challenge is to append values to the results variable. The first line declares the variable as an empty array.

Hi Greg, for some reason the Treehouse Code Machine as well as Xcode are providing an error to the code you provided -

Playground execution failed: Collections.xcplaygroundpage:99:16: error: use of unresolved identifier 'multiplier' results.append(multiplier * 6)

Not sure what this means

Hi Michael,

You need to keep the rest of your code! I just meant inside the loop, you can just write the code I had. If you don't have the while loop with multiplier, the compiler will have no idea what you're talking about :blush:.

Hi Greg - according to the array given it looks like that the value must be an integer - please, correct me if that's not the case. Still figuring this out (love it!)

Got it this way:

results.append(1*6)
results.append(2*6)
results.append(3*6)
results.append(4*6)
results.append(5*6)
results.append(6*6)
results.append(7*6)
results.append(8*6)
results.append(9*6)
results.append(10*6)

Thanks though!

Oh no! The purpose of this challenge is to use a loop. Imagine if you had to do 100 or 1,000 iterations - you absolutely should use a loop! This is exactly what they're for.

Try the challenge again with your original code, but replace your print statement with my append statement.

I should not laugh, but your reaction is too funny Greg! Yes, you are absolutely right, I should have used a loop there. I will go through the "Control Flow" course a couple more times since I do not feel entirely solid with them yet (even though I completed the course).

Thank you for all your feedback Greg, it is much appreciated!

Right on - I like your work ethic :blush:. Keep up the positive attitude, and you'll learn a ton!

Sorry for the late reply Greg, but your support there is much appreciated. Thanks for the great words - these are helpful!