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

Kurt Lyell
Kurt Lyell
2,892 Points

Code challenge expecting you to know how to do things that are not covered.

In the code challenge you need to find the odd numbers and if they are divisible by 7. The code you need to use is : if n % 3 != 0 && n % 7 == 0 { results.append(n) using the part n % 3 != 0 is not even covered in the tutorial. Finding odd numbers was never even covered, I dont even understand the != 0 part. I had to look up how to do this problem on stackoverflow.com

2 Answers

While I definitely understand the frustration of not being fully handheld, especially starting out, it's imperative you learn how to search for answers to questions on your own as well. I do a great deal of googling for things from basic to complex and each search allows you to learn something new.

Anyways - I don't know what the exact question was but from your question I assume it asks to create a function that accepts an array of numbers and returns those that are odd and divisible by 7.

The basis of your question works but I generally check for odd with modulo ("%") 2 instead of 3. The operator "!=" just means "not equals" so checking for odd like this, n % 2 != 0, can be written in plain english as: variable "n" modulo 2 is not equal to 0.

A function to define the parameters stated in your question could be something like this:

func findOddAndDivisibleBySeven(originalArray: [Int]) -> [Int] {
    var numbersThatAreOddAndDivisibleBySeven: [Int] = []

    for n in originalArray {
        if n % 2 != 0 && n % 7 == 0 {
            numbersThatAreOddAndDivisibleBySeven.append(n)
        }
    }

    return numbersThatAreOddAndDivisibleBySeven
}

findOddAndDivisibleBySeven([1,7,14,21,35,400,707])
//returns [7,21,35,707]
Jhoan Arango
Jhoan Arango
14,575 Points

Hello Kurt:

These operators are taught in Swift 2.0 basics. They show you the != (not equals), the % remainder operators, and the rest of the basic operators. Perhaps you skipped this lesson ? I've never ran into a challenge that makes you do something that has not been showed before.

The use of google, stackoverflow and other webpages including the The Swift Programming Language e-book are a developers best friends, no matter your level of experience.

Good luck with your challenges, and if you find something hard to understand, you can always post a question here, and someone will help you. I usually take my time crafting answers to those who need a deeper understanding on some topics, and I try to make it as clear as possible. So if you ever are curious to an answer from me, then you can go ahead and tag my name.

Have fun coding