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

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

2nd quiz challange

Hi I got stuck on 2nd quiz challenge. the problem said i have to print number * 6 for 10x looping and this is my code and what i tried

var results: [Int] = []

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

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

tried both and still got error.

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

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

3 Answers

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Reagen,

you were on the right track but you have two flaws in your code. Firstly you don't have to print the result but you have to append it to the results array. The second problem is that you don't have to interpolate the results of the multiplication because you're saving int values to the array and not strings.

So if you use the append method on the results array instead of print and append the results as integer values instead of strings it should work fine.

Here is a working example:

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

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

I hope that helps, good luck! :)

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

Hi Tobias,

Thank you for your help! I have another question for the test..

on the test it says that 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.

before this chapter the "constant" always refer to let for immutable. in this challenge we use for. so my question is is loop can be consider as constant?

thank you

Tobias Helmrich
Tobias Helmrich
31,602 Points

No problem! That's a really good question! This can indeed be quite confusing.

As far as I know and understand it, it is a constant and in a for in loop this constant (in this case multiplier) has a constant value for each iteration of the array (or in this case a range). That means that you can't change it inside of the loop but it will/can be changed for every iteration because it only has a constant/immutable value inside of the loop. I hope that makes it more clear and answers your question!

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

Hi,

I fully understand now..

Thank you Tobias!