BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Checkpoint 3 : Fizz Buzz using ternary operators

Forums > 100 Days of SwiftUI

Tried to make compact solution using just learned ternary operators. I was little surprised, that this code works.

var result : String

for number in (1...100) {

    result = number.isMultiple(of: 3) ? "Fizz" : ""
    result = number.isMultiple(of: 5) ? result + "Buzz" : result

    print((result.isEmpty) ? "\(number)" : "\(result)")

}

3      

@terhox learns a new trick

Tried to make compact solution using just learned ternary operators. I was little surprised, that this code works.

Well done! The term "ternary" may seem like a scary concept, perhaps like the terms "second derivitive" or "reduction sauce".

But it's really a concept you already know.... the IF statement
Instead of a multi line if, the ternary is compact, just a one liner.

You probably could squeeze both evaluations into one line. But I think that starts to hide the logic. Your solution is spot on.

Keep coding!

4      

hi,

once again, we have an AI jumping in with an "example in a language like JavaScript" which has nothing to do with the Swift language question (although it seems to work if you understand the JavaScript syntax) and from a user who joined the forum "today" ... a clear sign of an AI post.

... my goodness ...

but to @Terhox, thanks for asking the question, and thanks to @Obelix for responding (although i would question the notion of second derivative being a scary concept).

regards to both,

DMG

4      

Thank you @Obelix and @delawaremathguy for comments, and I wish great coding for you also. Coding is much more fun when sharing thoughts and solution ideas with other coders.

3      

In Checkpoint 3, the Fizz Buzz problem is elegantly solved using ternary operators. This concise approach employs the conditional operator to determine if a number is divisible by 3, 5, both, or neither. The ternary syntax streamlines the code, enhancing readability and demonstrating the efficiency of this logical paradigm in solving the classic Fizz Buzz challenge.

4      

I saw this post a month ago. And today just for fun I wanted to see if I could remember how to do this one. Turns out I got close and it actually worked(well at least I think it does). @Terhox Thank you for sharing the Ternary way of doing this. A slightly different way of doing it......

for x in 1...100
{
    let y = (x.isMultiple(of: 3)) ? "Fizz" : ""
    let z = (x.isMultiple(of: 5)) ? "Buzz" : ""
    print ("\(x) \(y)\(z)")
}

3      

@nyan  

I was wondering If I can upload my Checkpoint 3 work on the forum and have others review and give out new ideas to me. I'm at loss.

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.