Today’s code challenge is about being cool.
Problem Description:
A number is cool if it’s a multiple of 11 or if it is one more than a multiple of 11.
Return true if the given non-negative number is cool.
Test Cases:
Try this before you look at my solution.
Seriously – try it yourself.
Last chance.
Okay – here is my solution.
Here is the Ternary Operator again with a modulus operator used twice making it another one-liner.
Either condition would make the number a multiple of 11 or one more than a multiple of 11.
You can view my solution at the IBM Swift Sandbox.
Why do you use the ternary operator here? Everything before the question mark is already giving you the Bool you need 😉
By the way, if you would hypothetically need this often in your App, I’d even suggest the following which is more readable in the end:
extension Int {
var isCool: Bool {
return (self % 11 == 0) || (self % 11 == 1)
}
}
Usage:
22.isCool // true
23.isCool // true
24.isCool // true
Good catch. I like this form.