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

Gabriel Franco
Gabriel Franco
656 Points

honestly I'm idk where to start

need help

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

for n in 1...100 {
    if n is odd { print ( "okay")
    }

    // End code 
}

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Gabriel Franco,

You have the right idea. The challenge is asking you to check the number for two conditions. If it evaluates to true for both conditions, we will append it to the results array.

The first condition we need to check for is if the number is odd. We can do this by using the remainder operator and 2. If we divide the number by 2 and get a remainder of 0, the number will be even because it divided equally. So in order for the condition to be true for the case of an odd number it looks like this: n % 2 != 0

The next condition we need to check for is if the number is a multiple of 7. We can use the remainder operator and 7 here. If the number divides by 7 with no remainder (zero) then the number must be a multiple of 7.

We want to use the AND operator (&&) to check if both of these conditions are true before we append the number to the results array.

var results: [Int] = []

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

Good Luck!

Gabriel Franco
Gabriel Franco
656 Points

thanks a lot makes a lot of sense now