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

I have no idea how to do this. I know the remainder operator is %, but how do I reference it?

I don't know how to do this.

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

for n in 1...100 {
if n%2  
    // Enter your code below

    // End code 
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Derek Khanna,

In this challenge we are iterating over a range of Integer values, from 1 to 100, using a for in loop. The first time the loop runs, the first value in the range (1) will be assigned to the constant n. We can then use this constant inside of the loops body to check for some conditions.

The conditions we want to check for are:

1) n % 2 != 0

if the remainder of n divided by 2 is not equal to zero, n must be an odd number

2) n % 7 == 0

if the remainder of n divided by 7 is equal to zero, n must be a multiple of 7

We chain these two conditions using the AND operator &&. This ensures that both conditions must succeed for the operation to result in a true value.

If both conditions succeed, then we append the value of n to the results array.

Each time this loop runs, the value of n will be incremented to the next value in the range from 1...100.

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