UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 3: FizzBuzz in one line of code

Forums > 100 Days of SwiftUI

I am enjoying the updated lessons!

For checkpoint 3, Paul challenges us to solve the fizz-buzz problem.

I tried to do this in one line of code!

Copy paste into a playground.

for ๐Ÿบ in (1...150) { print ("\(๐Ÿบ) " +  (๐Ÿบ%3 + ๐Ÿบ%5 == 0 ? "fizzbuzz": (๐Ÿบ%3 == 0 ?  "fizz" : (๐Ÿบ%5 == 0 ? "buzz" : " ")))) }

Tell us. What do you think is clearer code?

๐Ÿบ.isMultiple(of: 3)

// or

๐Ÿบ%3 == 0 

Cheers!

31 October Update I reviewed this code and thought about using .isMultiple(of:) instead of the modulus operator. This produces the same output. But which code would be easier to review a few years from now?

for ๐Ÿบ in (1...150) { print ("\(๐Ÿบ) " +  (๐Ÿบ.isMultiple(of: 15) ? "fizzbuzz": (๐Ÿบ.isMultiple(of: 3) ?  "fizz" : (๐Ÿบ.isMultiple(of: 5) ? "buzz" : " ")))) }

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.