Swift 3 Challenge 4: Being Cool

cool-wallpaper-1

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:

Challenge-4-Test-Cases

Try this before you look at my solution.

Seriously – try it yourself.

Last chance.

Okay – here is my solution.

Challenge-4-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.

 

 

2 thoughts on “Swift 3 Challenge 4: Being Cool

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s