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

Ricardo Gonzalez
Ricardo Gonzalez
2,286 Points

I don't know how to finish my code.

I think I am doing right but don't know how to finish it.

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

for n in 1...100 {
   if n % 3 && n  {
   results.append(n)
   }
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Ricardo Gonzalez,

You're on the right track. You have the append part correct, we just have to look at the the conditions you are evaluating for. You need to check if each case of n is true for two conditions. These are if n is odd and if n is a multiple of 7.

To check if n is an odd number, we can use the remainder operator and 2. If the result is not equal to 0, this mean the number is not even because it did not divide by 2 with no remainder. We then use the AND operator to check if this number satisfies the condition where n is a multiple of 7.

To check if n is a multiple of 7, we do something very similar to checking if n is odd. Here we will use the remainder operator but this time with 7. We will check if the result is equal to zero. If it is, this means that the number was successfully divided by 7 without a remainder and that the number must also be a multiple of 7.

var results: [Int] = []

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

Good Luck

Ricardo Gonzalez
Ricardo Gonzalez
2,286 Points

Thanks Steven. I appreciate your help.