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 trialTaylor Farr
867 PointsHow do I append the results of the loop to the array?
So I am working in the Playground right now so I can see it run:
I currently have:
var results: [Int] = []
for multipler in 1...10 { multiplier * 6 }
results.append(WHAT GOES HERE)
*I thought it would be (multiplier) but that is not an identifier...
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
multiplier * 6
}
results.append()
2 Answers
Addison Francisco
9,561 PointsTaylor,
You want to append the value of multiplier * 6
to the results
array for every iteration of your for-in loop. Right now, you are calling results.append()
once, after the for-in loop is done executing. So, you need to move results.append()
into your for-in loop, then have your multiplier calculation be the parameter of .append()
It should look like this
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier * 6)
}
Magnus Hållberg
17,232 PointsThats where you put your calculation, multiplyer * 6. But results.append(multiplyer * 6) needs to be inside the body of the loop so it gets used for the amount of times you have choosen.