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

Ben Masel
Ben Masel
2,004 Points

I am not exactly sure what to do here, don't tell me the answer just tell me the steps and why.

I thought i knew what to do but this challenge is harder than i had anticipated!

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

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


    // End code 
}

1 Answer

Hi Benjamin,

The code for this is as follows,

var results: [Int] = []

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

What we are doing here is looping through a inclusive(i think) range of values between 1 and 100.

Using 'n' for each loop iteration we are checking if that value divided by 2 does not equal 0 meaning odd, and also if we divide the number by 7 it has no remainders, again 0 and meaning a multiple of 7. The % here is the modulo operator and looks for a remainder.

Once we have looped through the range, any numbers that are both even and divisible by 7 are then added/appended to our array.

Hope this helps. I'm still learning myself, so please let me know if you need clarification on anything else.

Marc