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

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 ...