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