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

Brian Uribe
Brian Uribe
3,488 Points

Help

This challenge question seems to be a huge jump in difficulty from the video. I'm not 100% sure where to start with this one.

Help would be much appreciated.

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

for n in 1...100 {



    // End code 
}

1 Answer

Michael Reining
Michael Reining
10,101 Points

Hi Brian,

What has really helped me is to always break down the challenges into individual components, test them in the Playground and then submit them once I get everything working in the playground.

Here is how I would break this challenge down.

var results: [Int] = []

for n in 1...100 {
    // Step 1 - find odd numbers
    // if n % 2 != 0, then number must be odd

    // Step 2 - find numbers divisible by 7
    // if n % 7 == 0, then number must be divisible by 7

    // Step 3 merge above into one if statement using && operator

    // Enter your code below
    if n % 2 != 0 && n % 7 == 0 {
        results.append(n) // Step 4 append number to array
    }
    // End code
}

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.

Now you can practice writing Swift code directly on your iPhone :-)

Code! Learn how to program with Swift