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

How to append results to an array?

I am having a problem in appending the results to the array. Can anyone please help.

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

for multiplier in 1...10 {
let multiple = multiplier * 6
results = [multiple]
}

3 Answers

Jordi GΓ‘mez
Jordi GΓ‘mez
3,568 Points

Hi Harsh,

To append the result to an array, you have to use ".append()".

Here you are:

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

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

You can also make the following since you don't use the multiple constant:

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

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

Thanks for your help Jordi. This worked for me.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

To begin with, you didn't need to change multiple to multiplier. And the method you're looking for is the append method. The first time through the loop the multiple will be 1 and we'll multiply it by 6 then append it to the results array. So at the end of the first time through the loop the results array will be [6]. At the end of the loop, the results array will be [6, 12, 18, 24, 30, 36, 62, 48, 54, 60]. Take a look at my solution and see if it makes sense:

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

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

Thanks a lot!

This worked for me

Thanks a lot Jennifer and Jordi !

Both of your solutions worked for me :D