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 trialSam Burchard
921 PointsHow do I get the results from my for in loop to display in the results variable without doing it manually?
//How do I get the results from this
for multiplier in 1...10 { print("(multiplier) times 6 is equal to (multiplier * 6)") }
//To display in this
var results: [Int] = []
2 Answers
Brandon Mahoney
iOS Development with Swift Techdegree Graduate 30,149 PointsYeah you definitely don't want to do it manually, computers are there to make tasks simpler right. You just need to use the append method of the array. Inside the braces place what it is you want to append. As below:
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier * 6)
}
Also to place code in a post use 3 back ticks and the word Swift above the code and then 3 back ticks right below the code.
```Swift
//Code goes here
``` //Then 3 back ticks like this below
Sam Burchard
921 PointsWow, thank you! You were SUPER helpful! :) I knew that I was missing something.
And thanks for the tip on putting code in a comment!
Brandon Mahoney
iOS Development with Swift Techdegree Graduate 30,149 PointsNo problem.
Batuhan Dogan
2,587 PointsHey! I've been trying to resolve this but it's not working.
var results: [Int] = [1,2,3,4,5,6,7,8,9,10]
for multiplier in results { print(multiplier * 6)
}
the error gives me is this.. " Make sure the results array contains the first 10 multiples of 6" Where am I doing it wrong?
Sam Burchard
921 PointsSam Burchard
921 PointsI was able to do it manually like this
for multiplier in 1...10 { print("(multiplier) times 6 is equal to (multiplier * 6)") }
var results: [Int] = [6,12,18,24,30,36,42,48,54,60]
But I had to enter the numbers manually, and I feel like I missed the point of the lesson.