Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,019 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,019 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.