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 trialHarsh Punjabi
463 PointsHow to append results to an array?
I am having a problem in appending the results to the array. Can anyone please help.
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
let multiple = multiplier * 6
results = [multiple]
}
3 Answers
Jordi Gámez
3,568 PointsHi Harsh,
To append the result to an array, you have to use ".append()".
Here you are:
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
let multiple = multiplier * 6
results.append(multiple)
}
You can also make the following since you don't use the multiple constant:
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier * 6)
}
Jennifer Nordell
Treehouse TeacherTo begin with, you didn't need to change multiple to multiplier. And the method you're looking for is the append method. The first time through the loop the multiple will be 1 and we'll multiply it by 6 then append it to the results array. So at the end of the first time through the loop the results array will be [6]. At the end of the loop, the results array will be [6, 12, 18, 24, 30, 36, 62, 48, 54, 60]. Take a look at my solution and see if it makes sense:
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier * 6)
}
Harsh Punjabi
463 PointsThanks a lot!
This worked for me
Harsh Punjabi
463 PointsThanks a lot Jennifer and Jordi !
Both of your solutions worked for me :D
Raymond Choy
iOS Development with Swift Techdegree Graduate 11,187 PointsWas the point of this exercise to change the multiple of an already existing times table to now be "times 6"?
Harsh Punjabi
463 PointsHarsh Punjabi
463 PointsThanks for your help Jordi. This worked for me.