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

San Francisco
San Francisco
28,373 Points

Swift 2.0 Collections and Controllers Challenge Task 1 of 2

In the challenge task here:

https://teamtreehouse.com/library/swift-20-collections-and-control-flow/control-flow-with-loops/for-in-loops

Can someone please provide a working example thanks

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

let multiplier = number 

for number in 1...10{
    print("\(number) times 5 is equal to \(number * 5)")
}

1 Answer

Hi, There are 2 problems in your code:

  1. You are assigning "number" to multiplier, which is not possible because the number variable is working only inside loop scope. Also, assigning an iterating value to a constant is not possible.
  2. You should add the resulted value to the array instead of printing it.

Below is my solution.

var results: [Int] = []

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

You are welcome