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

Yong Wha Hong
Yong Wha Hong
1,237 Points

Logical Operators Challenge

Why is this wrong for this challenge? It is saying I did not set the logical conditions correctly.

if n % 3 == 0 && n % 7 == 0 { results.append(n) }

3 Answers

Michael Reining
Michael Reining
10,101 Points

Hi there,

The challenge is looking for something different.

You are looking for all numbers that are divisible by 3 and 7. That is not what the challenge is asking.

It is asking that you find all odd numbers that are divisible by 7.

The code for that looks like this.

var results: [Int] = []

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

results

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app. :-)

Code! Learn how to program with Swift

can you break this down step by step I'm completely lost?

I think it's like this

n % 2 compares each of 1...100, which will result in some numbers with a remainder value. != 0 has a remainder that is not 0

n & 7 compares each of 1...100, which will result in some numbers with a remainder value. == 0 has a remainder of 0

&& both conditions apply

results.append(n) This will append the number of numbers found in 1...100 from the task (n) to results