Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Loops For In Loops

append value to the results array

Sorry guys, but it seems really difficult, or I am a complete noob.. ((( I've tried multiple combinations, but I do not understand how to append the values to the results array. It seems that "multiplier" is a string and just cannot be added. Should I add the values one by one? Then what is the sense of doing all these multiplications... Thank you

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

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

results.append("multiplier")
Steven Deutsch
Steven Deutsch
21,046 Points

You also may want to take a look at the type of the results variable, it's an array of Integers or [Int]. To append something to the array it needs to be the same type that the array stores.

// Here you are trying to append a string literal to an array, results, that stores Int values
results.append("multiplier")

Hope this clarifies somethings

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Eugene St,

No worries, everything seems difficult when starting out. You're close but what you need to do here is call the append method inside of your loop. Let's take a look at the code:

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

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

Here we have a for loop where we are iterating over a range of Int values 1 to 10. For each value in the iteration, we will assign a value to the constant named multiplier. Inside of the loop's body, we can now call the append method on the results array and pass it the value stored in multiplier.

Good Luck

Much appreciate, Steven. However now I get the following error: "Make sure the results array contains the first 10 multiples of 6" . Here is my code, can you please take a look on it and tell me what I am doing wrong?, sorry for disturbing you and thanks:

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

for multiplier in 1...10{ results.append(multiplier) print("(multiplier) * 6 is (multiplier * 6)")

}

Steven Deutsch
Steven Deutsch
21,046 Points

Not a problem Eugene,

Sorry, I don't have access to this challenge so I was unaware of what the question was fully asking.

The challenge wants the first 10 multiples of 6 to be stored in the results array. This means we need to multiply our multiplier value by 6 each time before appending the resulting value to the results array:

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

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

Hope this helps

I cannot believe that it was sooo simple ;) Thank you once again.