Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
An exercise challenge to create a function that takes in two parameters to find out if one is divisible by the other.
Challenge
Write a function that determines if a number is divisible by another. It will do the following:
 Accepts two Int parameters
 Figures out if the first parameter is divisible by the second
 Returns a Bool optional
 If the number is divisible it return a true else it returns a nil
 Call the function with 2 numbers and then print out whether “Divisible” or “Not Divisible”

0:01
As usual, we've covered a lot of ground in this course.

0:04
To reinforce all these new concepts, let's put them to good use.

0:09
Here's a challenge for

0:10
you, write a function to determine if a number is divisible by another.

0:16
It will do the following, accept two Int parameters.

0:21
Figures out if the first parameter is the divisible by the second,

0:26
returns a Bool as an optional, if the number is indeed divisible it

0:31
returns a true, else it returns a nil.

0:36
Finally, call the function with the two parameters.

0:39
And then print out, whether it is divisible or not divisible.

0:45
Don't worry.

0:46
These instructions are also below in the teacher's notes.

0:50
Remember, your main goal is to write this function to

0:53
find out if one number is divisible by another.

0:58
So, here's a big hint.

1:00
Use the remainder operator.

1:02
Okay, that's all I'm gonna say.

1:05
So, go head, pause this video, take a break, stretch,

1:10
get a drink, get comfortable, and then come back and give this problem a try.

1:16
If, for some reason, you're not able to solve it, then come back and

1:20
I'll show you how.

1:23
So, I've created a new playground,

1:24
and I've called this isDivisible, because that's gonna be the name of our function.

1:29
And I've copied here the instructions.

1:32
So, we need to create a function that accepts two int parameters.

1:38
And the function is going to be called IsDivisible.

1:41
So, we'll start with func, IsDivisible, and then the two parameters.

1:51
So, the number that you're going to divide by is usually called the dividend.

1:56
And it's going to be an Int.

2:02
And since we want to have named parameters,

2:04
I'm just going to put a hash there.

2:07
And the number we're going to divide by is called the divisor.

2:14
Remember in the instructions it said we return a bool optional, so

2:18
let's make sure we return the bool optional, and then put in curly braces.

2:26
So, now we need an if statement, so let's say if, dividend remainder operator.

2:35
Divisor is equal to 0,

2:40
then will return true,

2:46
else will return nil.

2:51
So, now we satisfy the bool optional return type.

2:56
We're returning true if, indeed, the dividend is divisible by the divisor.

3:02
If not, return no.

3:06
So, finally, let's call this function.

3:08
So, I'm going to use the if let syntax.

3:11
I'll say if let result equal isDivisble.

3:16
Now, there's a problem here,

3:18
I see that there's an error showing up here because I did not capitalize Int.

3:23
So let's make sure I capitalize Int here.

3:27
And now, we can use our isDivisible function.

3:31
So, we need to give it a dividend and a divisor.

3:34
So, I'll say 15 and three.

3:37
And it's gonna return a bool optional.

3:41
So, we can do, if let result is equal to,

3:45
so these numbers are divisible, so I'll simply print out divisible.

3:53
Else, we'll print out Not Divisible.

4:03
So, if we give it two numbers that aren't divisible, that says not divisible.

4:09
[SOUND] So, there you go.

4:13
It's pretty simple and straightforward and it utilizes pretty much everything that we

4:17
learned throughout this course except for Tuples of course.

4:21
But we've created a function.

4:23
We've created named parameters.

4:25
We're returning an option on.

4:27
And we even use comparative operators and math operators.

4:33
And finally we use the if let syntax to unwrap that optional.

4:38
So, congratulations if you were able to solve this problem.

4:42
Remember there's no right or wrong answer.

4:45
We're at the end of this course, to recap we learned about functions,

4:49
function parameters, return types, tuple's and optional's.

4:54
All very important concepts, in Swift that lay a good foundation.
You need to sign up for Treehouse in order to download course files.
Sign up