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

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