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

Gray Dorsett
Gray Dorsett
1,455 Points

How do you put the results in the array?

I'm stuck on this code challenge and wondering how I would put the results of the loop into the array.

Thanks!

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

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

1 Answer

Luke Dawes
Luke Dawes
9,739 Points

Hi Gray,

This Code Challenge is asking you to write a for-in loop that, for all the numbers between 1 and 10 inclusive, multiplies each number by six and then appends that number to the results array, which is an array of type Int.

In your code above, you've wrapped the value of multiplier in quote marks, meaning that the loop simply prints out ten strings saying "1 * 6", "2 * 6", "3 * 6" and so forth. Interestingly, your code passes in Xcode without the need for a print function, which I didn't know was possible!

You can use the append method on arrays to add things to them. What we're saying here is, for every multiplier in the range of numbers provided, take each multiplier and times it by six, and then append that result to the results array.

The code below passes for both parts of the Code Challenge.

var results: [Int] = []

for multiplier in 1...10 {

    results.append(multiplier * 6)
}

Hope that helps!