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 Collections and Control Flow Control Flow With Loops For In Loops

Erik Luo
Erik Luo
3,810 Points

How do you append the value to results?

How do i append the value to results? and how come in this challenge there is a Int array(var results: [Int] = []), but when I tried it in xcode, it gave me an error.

loops.swift
// Enter your code below
var results: [Int] = []
let number = 1...10
for multiplier in number {(multiplier * 6)}
results.append()

1 Answer

andren
andren
28,558 Points

Your solution is very close to correct, in fact you have all of the code for the solution written. You can solve the challenge by simply rearranging your code a bit.

To append something to an array you can indeed simply use the .append method, just like you do in your solution, however you need to pass in the value that you want to append as an argument to the method. In addition you have to append the results within the loop, currently your appending code is outside the loop.

If you move it inside the loop, and use the value that is already written in the loop as the argument like this:

var results: [Int] = []
let number = 1...10
for multiplier in number {
    results.append(multiplier * 6)
}

Then you will be able to pass the challenge.

Erik Luo
Erik Luo
3,810 Points

OMG, I didn't know you can do that, so close. Thank you!