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 Functions and Optionals Optionals Exercise: isDivisible function

Matthew Glick
Matthew Glick
2,638 Points

Challenge: Does my code work?

// was wondering if this works for the challenge

func numbers (numberOne: Int, numberTwo: Int) -> Bool? { let numbers = 1...100

if (numberOne % numberTwo) == 0 {
    return true
}
    else if  (numberTwo % numberOne == 0) {
        return true
    }
    else {
        return nil
    }
}
Jhoan Arango
Jhoan Arango
14,575 Points

Matthew : You have the Idea but I will give you a hint. Create two functions instead of one.

if you need more hints let me know :)

1 Answer

The code will compile and somewhat has a use, but it doesn't work for this particular challenge. First off, what is "let numbers = 1...100" supposed to mean, or what is the use of it? If you are trying to make a list called numbers from 1 to 100 then you can't do it like that.

Second, your function checks if the first number is divisible by the second number, and returns true if so, and checks if the second number is divisible by the first, and returns true as well. This is not very practical because it doesn't specify whether the result was "The first number is successfully divisible by the second number" or vice versa.

Lastly, the challenge simply asks you to make a function that only returns true IF the first number passed is not divisible by the second number. It should look like this: func isNotDivisible (numberOne: Int, numberTwo: Int) -> Bool { if (numberOne % numberTwo != 0) // if it doesn't equal 0 then numberOne is not divisible by numberTwo { return true } else { return false }

}