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 2.0 Basics Swift Operators Working With Operators

Al Hughes
seal-mask
.a{fill-rule:evenodd;}techdegree
Al Hughes
iOS Development Techdegree Student 786 Points

Operators quiz in Swift Basics 2.0

I have completed the quiz with help from the forum. I just don't understand what any of it meant. I feel like its a waste if I don't understand what I actually did. Can anyone break down the question for me and help along with understanding why the specific code was written?

I appreciate any help that you awesome peeps can provide.

2 Answers

For Task 1 you are asked to use the % (modulus or remainder) operator to get the remainder of dividing value by divisor.

// Task 1 - Enter your code below
let result = value % divisor
// Task 2 - Enter your code below
let isPerfectMultiple = result == 0

For Task 2 you are asked to see if result is equal to 0, and assign the result (a Bool) to isPerfectMultiple

Al Hughes
seal-mask
.a{fill-rule:evenodd;}techdegree
Al Hughes
iOS Development Techdegree Student 786 Points

Ok just to clear up my confusion. I'm letting the computer to answer the question. My code is only asking me to write out the correct question. Is that correct?

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Ok, I'll give it a shot!

Challenge Task 1 - step 1 The first step is fairly straight forward. Make a constant named result and store the result of the value mod the divisor. Given that the value and divisor are set by Treehouse as 200 and 5 respectively the result will be equal to 0.

let result = value % divisor

Challenge Task 1 - step 2

Step two requires that you make a constant named isPerfectMultiple. Here you're going to store a boolean value which is the outcome of comparing the result value to 0. We're just asking this: "Was the value evenly divisible by the divisor? If it was store true in isPerfectMultiple".

let isPerfectMultiple = result == 0

Challenge Task 2

Here they've set up two operations that are fairly complex so we're going to let the computer tell us which result is bigger. We declare a constant called isGreater and we assign the boolean outcome of the comparison between someOperation and anotherOperation. If someOperation is bigger then isGreater is true, otherwise isGreater is false.

let isGreater = someOperation > anotherOperation

Hope this is helpful!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Al Hughes yes. See these lines?

let someOperation = 20 + 400 % 10 / 2 - 15
let anotherOperation = 52 * 27 % 200 / 2 + 5

Who wants to calculate those in their heads? We're going to let the computer do that and then assign the result back into someOperation (which has its own calculation). And anotherOperation (which has its own operation). Then we're simply going to ask the question "Was the number generated by someOperation bigger than anotherOperation?". And we do that with this line:

let isGreater = someOperation > anotherOperation

If someOperation was greater than another operation then true will be stored in isGreater. Hope that clarifies things!