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

Alysa Wakefield
Alysa Wakefield
689 Points

Stuck on challenge For In Loop. Unsure how to also define constant called multiplier after printing.

I think i have the first bit of the code correct?! but then it asks the next question "For in loops also define a constant that temporarily stores the value in the iteration process. For the loop you're writing name this constant multiplier.

I dont know how to link my number calculation now with this constant.

-also i cant find anyone else working on the 6 times table!? everyone else in the community seems to be working on the 7 times which makes it hard to learn from the community, i think they're working on a different question !?

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

5 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I think there might be some misunderstanding of the instructions. The array should start off as empty. But you've filled it up with 1 to 10. We're going to use our for in for those numbers. What we're going to do is every time through the loop we're going to take that number which should be named multiplier and multiply it by 6. Then we take the result and append it to our (currently empty) array. This will mean that at the end... the results array will look like this [6, 12, 18, 24, 30, 36, 42, 48, 54, 60]. We never print anything at all.

var results: [Int] = []

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

So here we have our empty results array set up. Now we have something called multiplier which you can think of as a temporary variable. The second this for loop ends, it ceases to exist. So at the first pass multiplier is equal to 1 and we multiply it by 6 and put it in our array. Our results array now looks like this: [6]. On the second pass, multiplier is equal to 2 and we multiply it by 6. We take the result and put it into the array too. Now our array looks like this: [6, 12]. We do this all the way up to 10. I hope this helps! :sparkles:

Brian Kasper
Brian Kasper
464 Points

Thank you this was very helpful

Alysa Wakefield
Alysa Wakefield
689 Points

Thank you :) Sometimes i feel like there is a huge jump between what we have just watched on the video and the challenge itself.

Appreciate the speedy reply!!! Its great to get a fast reply when you've allocated all sunday to learning, it would be a shame to stop at the first hurdle and have to wait a couple of days for a reply!

Alysa Wakefield
Alysa Wakefield
689 Points

Sorry i do have one more question.. it all kinda makes sense now that you can see the answer, i dont think i ever would have figured that out on my own though. However the question/task asked us to create a 'constant called multiplier' in all our other tasks when you created a constant you always said "let multiplier = " f.ex . so how come that doesn't apply here?

thank you!!!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yes you're correct. But as I stated above:

Now we have something called multiplier which you can think of as a temporary variable. The second this for loop ends, it ceases to exist.

I don't know what to say other than many languages have this same sort of construct for looping through arrays/lists/collections where the amount of data is unknown. Again, this is a temporary variable. When that for loop stops running, you will no longer have access to the multiplier variable/constant. So just for future reference, Swift and other languages use this sort of temporary variable when executing loops like this. This is not the only place you'll see this. Hope this helps! :sparkles:

Alysa Wakefield
Alysa Wakefield
689 Points

ahh okay! thank you so much again! I think im going to need to restart this whole video segment with this new knowledge :)