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

Sandesh sardar
PLUS
Sandesh sardar
Courses Plus Student 3,042 Points

I am not getting what exactly I need to code, question is not clear for me

I have done first step, calculate result and store it in required variable. Not getting what exactly i need to code

operators.swift
// 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 % 5
// Task 2 - Enter your code below
result == 0

let isPerfectMultiple = result

1 Answer

Hi Sandesh,

" Step 1: Using the remainder operator, compute the remainder given the value and a divisor. Assign this value to a constant named result. "

Step 1 of the task requires that you use the constants 'value' & 'divisor' in the computation. You're almost there but you are using the value of the divisor constant, rather than the name of the constant. When you use a hard coded value like that it is referred to as a 'magic number' i.e. it is bad practise as it is not always clear what that number represents. It might make sense to you now but if you were to look over code that was full of these magic numbers in the future, there is a good chance you would have forgotten what the numbers represent. Avoiding the use of hard coded data will make your code more readable and maintainable.

"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."

Step 2 requires that you compare the value of the constant you defined in step 1 to zero. To do this you use the equality operator == which compares the values on both sides and test for equality. This is a boolean condition, which will evaluate to either true or false. What you have done in your answer is assigned the value of result to a new constant named isPerfectMutiple. You have not tested for equality.

See code snippet below for the correct answer.

// 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 = (result == 0)