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 trialAbdijabar Mohamed
1,462 PointsHelp me debug my code
var results: [Int] = [] let multiplier = for x in 1...10{x*6} results.append(multiplier)
In iterating over the array, what is wrong with my code? Thanks
4 Answers
Ella Chiang
19,257 PointsYour code:
var results: [Int] = []
let multiplier = for x in 1...10{
x*6
}
results.append(multiplier)
Get rid of let
. In a for in loop, the constant multiplier
goes between the for and the in. Don't need the x since multiplier is the name used for every item when iterating through the array. Also, results.append()
goes inside the for in loop.
My suggestion:
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier * 6)
}
Abdijabar Mohamed
1,462 PointsThank you so much Ella. Very much appreciated.
Ella Chiang
19,257 PointsYou're welcome. Happy to help and thanks for the follow.
Abdijabar Mohamed
1,462 PointsI actually followed you on linked In. Abdi
Kenneth Fong
1,729 PointsHey Ella, I didn't have any problem wrong with this question, but it helped! Thanks for sharing your knowledge!