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 trialMike Bailey
823 PointsSwift 3 basics code quiz stumped me
A Swift 3 basics code quiz stumped me with following question: — In the editor below, you have two constants - value and divisor. Step 1: Using the remainder operator, compute the remainder given the value and a divisor. Assign this value to a constant named result. Step 2: When value obtained using a remainder operator is 0, this means that the value is a perfect multiple of the divisor. Compare the value of result to 0 using the equality operator and assign the resulting value to a constant named isPerfectMultiple. — I searched for the answers in these forums and I found the correct answers (in code below):
// Enter your code below let value = 200 let divisor = 5
let someOperation = 20 + 400 % 10 / 2 - 15 let anotherOperation = 52 * 27 % 200 / 2 + 5
// Task 1 - Enter your code below let result = value % divisor
// Task 2 - Enter your code below let isPerfectMultiple: Bool = (result == 0)
—
But rather than simply copying/pasting the correct answer I'd like to understand how these answers come about. Can someone break down these results for me?
Thanks :)
// Enter your code below
let value = 200
let divisor = 5
let someOperation = 20 + 400 % 10 / 2 - 15
let anotherOperation = 52 * 27 % 200 / 2 + 5
// Task 1 - Enter your code below
let result = value % divisor
// Task 2 - Enter your code below
let isPerfectMultiple: Bool = (result == 0)
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! Well let's take it step by step, shall we? The first part should be fairly straight-forward given how long you've come in the course. We declare a constant named result
and assign the remainder (or modulus) of the value
divided by the divisor
variable. That's where we get this line:
let result = value % divisor
Now the next part is to set up a new constant named isPerfectMultiple
that will hold a boolean value. Because the value is 200 and divisor is 5 as hard-coded in the beginning, you can probably answer this yourself. Is 200 a perfect multiple of 5? Yes, it is. When we multiply 5 by 40 we get a result of 200. Another way to say this is that 200 is evenly divisible by 5.
While the line you've copied will pass, it does have some things that aren't actually needed.
Mine looked like this:
let isPerfectMultiple = result == 0
The value held in result with either be a 0 or not. Here we need to understand the modulus operator. Take a look at an example:
- 1 % 3 = 1 .... one divided by three equals 0 with a remainder of 1
- 2 % 3 = 2 ... two divided by three equals 0 with a remainder of 2
- 3 % 3 = 0... three divided by three equals 1 with a remainder of 0
- 4 % 3 = 1 ... four divided by three equals 1 with a remainder of 1
Seeing the pattern here? Anything that has a remainder of 0 is evenly divisible by the number on the right side of the %
sign. So if 200 is evenly divisible by 5 or the result
is equal to 0 then it is a perfect multiple. The value of the expression will be assigned back into isPerfectMultiple
as a boolean.
Hope this helps clear things up!
Mike Bailey
823 PointsMakes sense.
Thanks for the explanation :)
Peter Burkhard
2,853 PointsPeter Burkhard
2,853 PointsThank you!