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

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.