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

iOS

Kent Lou
PLUS
Kent Lou
Courses Plus Student 2,185 Points

My 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
Greg Kaleka
39,021 Points

Hi 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 :beers:

-Greg

Kent Lou
Kent Lou
Courses Plus Student 2,185 Points

Thank 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
Greg Kaleka
39,021 Points

You'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
Kent Lou
Courses Plus Student 2,185 Points

Right! I see the difference now, thanks a lot Greg.