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 Review: Functions

hans wagner
hans wagner
4,872 Points

isDivisible function works in Playground but not is code challenge

Here is my code: func isDivisible (dividend: Int, divisor: Int) -> Bool? { if dividend % divisor == 0 { return true } else { return false } }

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

if let answer = isDivisible(15, divisor: 3) { print("Divisible", terminator: "") } else { print("Not Divisible", terminator: "") }

if let result = isNotDivisible(15, divisor: 3) { print("Not Divisible", terminator: "") } else { print("Divisible", terminator: "") }

divisible.swift
func isDivisible (dividend: Int, divisor: Int) -> Bool? {
    if dividend % divisor == 0 {
        return true
    } else {
        return nil
    }
}

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

if let divisible = isDivisible(15, 3) {
  print("Divisible", terminator: "")
} else {
  print("Not Divisible", terminator: "")
}

if let result = isNotDivisible(15, 3) {
  print("Not Divisible", terminator: "")
} else {
  print("Divisible", terminator: "")
}

3 Answers

The question is looking for your method to return a Boolean, not an Optional - so if you amend your lines return nil to return false that may assist.

Also, you've duplicated pretty much the whole method. The two methods are a character, or so, different. How about calling isDivisible from inside isNotDivisible and negating the returned value?

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

func isNotDivisible(#dividend: Int, #divisor: Int) -> Bool {
    return !isDivisible(dividend: dividend, divisor: divisor)
}

That's more DRY.

Hope that makes sense.

Steve.

hans wagner
hans wagner
4,872 Points

Steve, thanks for your quick reply. I like your DRY approach, as I recognized the two methods were very similar. Though that is what we were asked to do. Thanks again for your help.

The question asks for a method that returns a boolean depending whether the divisor and dividend are not divisible. How you do that is wholly up to you - there's no requirement to repeat code.

I think the Treehouse lessons make a strong point of refactoring to avoid repetition but it isn't always easy to see how to achieve it. Thankfully, this challenge will pass with both ways of achieving the result so the duplicated methd is fine, and it is only a few lines of code.

Steve.

hans wagner
hans wagner
4,872 Points

I appreciate striving being efficient in one's code. Thank you for encouraging me to pursue the best approach.