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

isDivisible function challenge

I had this code and I didn't have any errors and I got the output I wanted. Is it wrong?

import UIKit

func divisibleTest(firstNumber: Int, secondNumber: Int) -> Bool? {
    if (firstNumber % secondNumber == 0) {
        return true
    } else if (firstNumber % secondNumber != 0) {
}
    return nil
}

divisibleTest(6,6)

if let result = divisibleTest(9,3) {
    println("Divisible")
} else {
    println("Not Divisible")
}

Ahahaha. That's funny. I just realized I had an extra line of code I didn't need in my else if statement in the function. I wasn't getting any errors but I was wondering why the playground output "2 times". I'm very relieved to know I understood this exercise. I did leave out the labels though. Does that matter?

2 Answers

Your code looks fine, except for the unnecessary if-else statement which you already realized. You don't need external parameter names in a function. When someone calls the function, they'll see the local parameter names when they start typing the function call, so it's not that necessary for the function caller. However, after the function call has been made, without the external parameter names in the function call, it's not that clear to someone reading the function call what each value represents. So external parameter names make cleaner code.

I'm just learning so i may be wrong but it seems that your code is not returning a true value but a some true value

I did this but imo it's a bit borderline it's not the same function that return the value if it's true or nil : ``` func isDivisible (#numb1: Int, #numb2: Int) -> Bool? { if (numb1 % numb2 == 0) { return true } println("Not Divisible") return nil }

if let iss = isDivisible(numb1: 9, numb2: 3)?.boolValue { iss if iss == true { println("Divisible") } }

plus i wanted to print 9 is divisible by 3

so i tried lot of things but i didn't get the value in my print someone know how to do ? sorry but the ```aren't working ...