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

print the loop

Not sure about this question. All I can do is print the results in the debug area at the bottom, not each individual loop. ???

Challenge Task 2 of 2

Inside the body of the loop, we're going to use the multiplier to get the multiple of 6. For example, if the multiplier is 1, then the multiple is 1 times 6, which is equal to 6.

Once you have a value, append it to the results array. This way once the for loop has iterated over the entire range, the array will contain the first 10 multiples of 6.

/Users/mattconway/Desktop/Screen Shot 2016-01-13 at 1.29.54 PM.png

loops.swift
// Enter your code below
var results: [Int] = []
for number in 1...10 {
  let multiplier = number; print("\(multiplier) times 6 is equal to \(multiplier * 6)")
  }
for multiplier is 1...10 {
  print multiplier("\(multiplier) times 6 is equal to \(multiplier * 6)")
  }

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Matt Conway,

What you want to do is use multiplier as the temporary constant in your for in loop, instead of number. This will take each number in the range of 1 through 10 and assign it to the temporary constant called multiplier. You can then use multiplier inside your function body.

For example, for the first iteration of your loop, the multiplier value would be equal to 1. Once the statements inside of the loop are finished, the loop will run again and assign the next value, of 2, to the multiplier constant. This process will repeat until the loop has gone over all the values of 1 through 10.

So, inside of your loop body, you will need to write a statement using the append() method that takes the value of multiplier and adds it to the results array. The loop will continue to update the value of multiplier for each iteration of the loop.

Your solution should look like this:

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

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

So you can see how this is working:

// First iteration, this code runs and multiplier is set to 1
for multiplier in 1...10 { 
    results.append(multiplier * 6) // multiplier is now equal to 1
}
// Second iteration, the same code runs again but multiplier is now 2
for multiplier in 1...10 { 
    results.append(multiplier * 6) // multiplier is now equal to 2
}
// Third iteration, the same code runs again but multiplier is now 3
for multiplier in 1...10 { 
    results.append(multiplier * 6) // multiplier is now equal to 3
}

Hope this helps!