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 trialVidur Bahl
Courses Plus Student 1,440 Pointshow insert all numbers in this array ??
how to write codes for inserting values in a array ??
// Enter your code below
var results: [Int] = []
for multiplier in 1...10{
results = [multiplier * 6]
}
2 Answers
Stone Preston
42,016 PointsCurrently you are assigning an array to results. So each time the loop runs the value of results is just overwritten with a new array value. Thats not want you want. You want to APPEND a new element onto the array, not assign it a new value each time
you need to append the result onto the array using either the append method, or the += operator:
// Enter your code below
var results: [Int] = []
for multiplier in 1...10{
results += [multiplier * 6]
}
or
// Enter your code below
var results: [Int] = []
for multiplier in 1...10{
results.append(multiplier * 6)
}
for more information on arrays see the swift documentation
Vidur Bahl
Courses Plus Student 1,440 Pointshello stone,
But with this code too array does not include array of [ 6,12,18,24,30....till 60]
it only shows 60....how to show all values in array using loop function ??