NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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?

   

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

1      

Ey @Obelix!

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

Thanks for your review!

   

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

1      

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.

2      

Great! 💪

   

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? ^^

   

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.

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.