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

George Thomas
George Thomas
768 Points

i cannot append this information to an array

i am trying to append the 6 times table (from 1-10). it keeps bringing up an error how to i do this?

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

for multiplier in 1...10 {
    var answer = (multiplier *  6)
    print ("\(multiplier) times 6 is equal to \(multiplier *  6)")

result.append (answer)



}

The code should compile and run, what is your expected result? An array with 10 numbers [6,12,...60]?

4 Answers

Are you certain the array your are appending to is the right name? In my challenge it shows the array defined as results but you append to result.

William Humphrey
William Humphrey
5,543 Points

You don’t need to create a var answer or use a print statement. The last part is VERY close. Don’t overthink the problem.

George Thomas
George Thomas
768 Points

So should it be:

var results: [Int] = []

for multiplier in 1...10 { results.append("(multiplier) times 6 is equal to (multiplier * 6)")

}

I get an error of:

swift_lint.swift:11:20: error: cannot convert value of type 'String' to expected argument type 'Int' results.append("(multiplier) times 6 is equal to (multiplier * 6)") ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You're appending the string to an integer array. All that needs to be appended to the array is the multiplication of multiplier * 6.

Everton Carneiro
Everton Carneiro
15,994 Points

You just misspelled the array name, It's results instead of result. Also you don't need to create a print statement for this task, the task is only to append the values to the arrays. Also It's not wrong, but you don't need to create a variable to store the values, you can simply do like this:

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

Keep in mind that is this challenges you have to type exactly the same name as It's required, otherwise the challenge will not accept your answers, even if your code is write.