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

how is this not right?

I'm doing the last question of the optionals and we have to write a function thats opposite to the one before. so instead of checking if the numbers are divisible we'll check if they're not.

here's what i did....

func isNotDivisible (dividend: Int, divisor : Int) -> Bool { if dividend % divisor > 0 { return true } else { return false }


It gives me an error and i don't know why... PLEASE!! does anybody have an opinion. I only copied the second function and i am assuming we leave the first function (isDivisible()) in the code.

5 Answers

It must be a function like this:

func isNotDivisible (#dividend: Int, #divisor: Int) -> Bool {
    if dividend % divisor != 0 {
        return true
    } else {
        return false
Szymon Kadłuczka
Szymon Kadłuczka
8,762 Points

First of all, the return type should be optional, so Bool? Secondly, if the statement is not true, return nil. Then you can use if-let.

Roger Martinez
Roger Martinez
3,239 Points

What is the error you are seeing?

One issue I see is that you're testing for the remainder being greater than 0. It looks like the % operator can return a negative remainder (not the mathematical version of the mod function). So:

println(-9 % 4)

answers -1

So, your function might return a wrong answer if the remainder is less than 0.

Also, since the function you're reversing just returns true or false, you could reverse that with just:

return !isDivisible(dividend, divisor)

Actually the condition in the if statement was the wrong thing. i guess the " if dividend % divisor > 0" wasn't the way it was supposed to be. Thanks "Avalon community" i just changed the condition to " if dividend % divisor != 0" and now it worked! Thank you so much for the help guys