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 Swift Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

Got and error that say to make sure I'm appending the proper value but my code seems correct. What am I doing wrong?

I've tested this code in Xcode in a Playground and it seems to work. So what's the problem?

operators.swift
var results: [Int] = []

for n in 1...100 {
    // Enter your code below
    if n%7 == 0 && n%2 != 0 {
        results += [n]
    }
    // End code 
}

This is the exact error I got: Bummer: Make sure you're appending the correct values to the results array in order to pass the challenge

1 Answer

Magnus Hållberg
Magnus Hållberg
17,232 Points

Theses challenges are very picky about the details, all that's wrong is that's its in the wrong order. First you try if its odd and then if its a multiple of 7. You have done the other way around.

I don't think it matters if I check odd first and then if its a multiple of 7 because I use the && operator. So both conditions must be true.

But I was able to solve this issue. The mistake was using "results += [n]" to append the integer to the array. It should be correct but the online compiler didn't accept it. So I changed the code to use ".append" method and it worked!

for n in 1...100 { // Enter your code below if ((n%7 == 0) && (n%2 != 0)) { //results = results + [n] results.append(n) } // End code }

Thank you for your help!