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 trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Kent Lou](https://secure.gravatar.com/avatar/e9b9a86e244f79c69fca5d676accb940?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Kent Lou
Courses Plus Student 2,185 PointsMy code seems to give me correct answers in xcode playground but couldn't get pass the challange
I guess the problem is to do with how I appended the results but I just don't understand why this works in xocde but couldn't get pass challange
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 != 0 && n % 7 == 0 {
results += [n]
}
// End code
}
1 Answer
![Greg Kaleka](https://secure.gravatar.com/avatar/54c2cac346df05d7b70cc0a6825c1cba?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Greg Kaleka
39,021 PointsHi Kent,
You should use the array method append
. The rest of your code looks good. Just change your inner line of code to:
results.append(n)
One other suggestion: your if statement is a bit hard to read, and I would consider adding parentheses. It's not wrong the way you have it, but I think this would be more clear:
if (n % 2 != 0) && (n % 7 == 0) {
Cheers
-Greg
Kent Lou
Courses Plus Student 2,185 PointsKent Lou
Courses Plus Student 2,185 PointsThank you Greg! I got passed the challenge with the append method, just confused on why the += operator gave different results compared to the append method (since they were presented as equivalent in earlier videos on swift2.0 track)
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsYou're right that += will work, but there's no sense in creating an array with one element in it just so you can add it to another array. That's what append() is for. Oftentimes the code challenges will only accept one way of doing things - sometimes that's not great... in this case, I think it's fine, since using the append() method makes a lot more sense.
Kent Lou
Courses Plus Student 2,185 PointsKent Lou
Courses Plus Student 2,185 PointsRight! I see the difference now, thanks a lot Greg.