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

task 2 of review : functions swift

i'm don't know what to do

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

if let result = isNotDivisible(dividend: 4 , divisor : 3){
         return ""
}

1 Answer

Hi there,

In this challenge you are given a function called isDivisible which takes two numbers as parameters and let's you now if one is exactly divisible by the other, or not. The function returns true or false, as appropriate.

The first function looks like:

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

The challenge is to make the opposite function, i.e. create a function called `isNotDivisible that takes two parameters and returns true if they aren't divisible and false if they are. There's two ways of achieving this, one adheres to D.R.Y. and the other doesn't.

If you simply reverse the condition that determines what the function returns, i.e. you change the test in isNotDivisible from:

if dividend % divisor == 0

to

if dividend % divisor != 0

Then that will fit the requirements. That's not very slick, though - you are repeating the entire function, virtually. So, how about using the existing function but opposing what it returns. So:

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

That just returns the opposite of the first function which is what the challenge is asking for.

Hope that helps!

Steve.