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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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

Greg Kaleka
Greg Kaleka
39,021 Points

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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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.

Greg Kaleka
Greg Kaleka
39,021 Points

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.

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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

Greg Kaleka
Greg Kaleka
39,021 Points

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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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!

Greg Kaleka
Greg Kaleka
39,021 Points

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.

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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!

Greg Kaleka
Greg Kaleka
39,021 Points

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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

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