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

Checkpoint 3 - a cleaner solution

Forums > 100 Days of SwiftUI

Hi, I think this solutions is a bit cleaner than the proposed in the video. It prevents the nested code and the duplication of the check for the case 5:

for i in 1...100 {
    var text = ""

    if i.isMultiple(of: 3) {
        text += "Fizz"
    }

    if i.isMultiple(of: 5) {
        text += "Buzz"
    }

    print(text.isEmpty ? i : text)
}

What do you think?

2      

Nice job! Indeed it is cleaner.

Here'a challenge for you. Think about your solution, and reduce it to one line of code!

Try it first before looking at > FizzBuzz in One Line

3      

Ey @Obelix!

It's cool you solutions, very interesting to play with it. But it's more difficult to read.

Thanks for your review!

2      

Making that a one-liner is just mean to whomever follows you on future edits. After 35 years doing this, readable, maintainable code wins every single time. "Clever" gets nothing but contempt during a code review, updates, or bug fix. :D

3      

Antonio chides:

But it's more difficult to read.

Chris agrees with:

Making that a one-liner is just mean to whomever follows you on future edits. After 35 years doing this, readable, maintainable code wins every single time.

Hey! I agree with both of you. I was just trying to have fun and test the boundaries.

We had a lunchtime coding contest at work last year, and I submitted a similar entry, for fun.

4      

Great! 💪

2      

Hi sorry to bother you i really liked that approach

But i cant really understand the print call. Could you maybe break it down for me? ^^

2      

Remember both codes (Antonio's above and Obelix's linked) basically return the same thing - multiples of 3 are Fizz, multiples of 5 are Buzz and multiples of 15 are FizzBuzz.

The key for me to understand Obelix's code was to replace the 🍺 with an i (that is what Antonio uses above). This is just a variable name and it can be anything you want.

Personally, I think Obelix's version is hilarious and awesome! Can't wait to use this somewhere in my code just for the fun of it.

3      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.