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

Comparison

What is wrong with this code?

// 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 result == 0 let isperfectMultiple = 0

3 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

What they are asking you to do with the two steps in the first task is to first set the constant result to the value % divisor, which you have done correctly. But what they are asking you to do with the second step in that first task is to assign the result of the equality operator to the constant isPerfectMultiple. So that would mean the boolean output of the equality operation would be assigned to the constant.

It would look like this.

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

Thanks Nathan ..Appreciated.

thanks, but how did you figure it out from the exercise requirement?

Nathan Tallack
Nathan Tallack
22,160 Points

So task one of the challenge had this description.


For the first task, we're going to keep it pretty simple and check if we know how to use the reminder and equality operators.

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.

Submit your answer after you've completed both steps 1 and 2.


They provided this code in the template.

// 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

// Task 2 - Enter your code below

For step 1 of this task we assigned the remainder to the result value. Hence this line of code.

let result = value % divisor

For step 2 of this task we had to get that result and ensure it is 0. This would indicate that the value is exactly divided by the divisor with nothing remaining. Hence this line of code.

let isPerfectMultiple = ( result == 0 )

They make most of these challenges simple by breaking down challenges into tasks and tasks into steps. In this way they are also teaching us good coding practices. Break things down into smaller and smaller tasks and steps so that it is easy to code and easy to debug. Less overwhelming that way. ;)

thank you for your replies, that's amazing of you, I got it finally and found that out in my exercise eventually, however i feel (and that's really just feedback at this point because I love your courses) that isPerfectMultiple = (result == 0) came out of nowhere in this exercise compared to all previous exercises logic flow perhaps some of this might become a bit clearer in the exercise statement.

thank you again for your help!