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

for in loop

Not sure how to get this running ...

Challenge Task 1 of 2

For this challenge, we're going to use a for in loop to compute the multiplication table for 6 and append the results to an array.

To start off, I've created an empty array of type Int and assigned it to a variable named results. Note that we have to explicitly declare the type here so that the compiler knows this array will hold Int values.

Your task for this step is to create a for in loop that iterates over a range. Multiplication tables typically range from 1 to 10 (with 10 included) so the range you are iterating over should go from 1 to 10.

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. /Users/mattconway/Desktop/Screen Shot 2016-01-13 at 11.47.56 AM.png

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

1 Answer

It is just asking for you to assign number to the constant multiplier. It is a little contrived.

var results: [Int] = []
for number in 1...10 {
  let multiplier = number
  print("\(multiplier) times 6 is equal to \(multiplier * 6)")
}

It didn't work at first, according to playground there is supposed to be a semicolon after number (before print) cuz it says there cannot be two consecutive statements. Now it works!