As the title says, make a classic FizzBuzz function or method without using if/else (or equivalents like ternaries, ?:, sneaky).
Specifically:
- The function should accept one argument, assume it will always be a positive integer.
- The function should return a string (or something coercible into a string in loosely typed languages) according to the following rules:
- If the given number is divisible by
3, then returnFizz - If the given number is divisible by
5, then returnBuzz - If the given number is divisible by both
3and5, then return the combinationFizzBuzz - If the given number is none of those things, then return the given number
- If the given number is divisible by
The expected outputs for the first fifteen numbers in order is:
1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz
Hard mode
Do this without "secret" conditionals like || and && (in loosely typed languages like JavaScript) or null coalescing operators like ?? or null safe operators like ?. or &..
Also no looping constructs that could be abused into a conditional like while or for.
Hint number 1
I tagged functional on this post because functional programming can be used to solve this.
Hint number 2
I tagged oop on this post because the original object oriented concepts of message passing can be used to solve this.
Post below with your answers!
Think this is impossible? I'll post my own answers in both FP and OOP styles next week.
(If you were hoping for the next installment of my With Only CSS series, styling radio buttons, that's coming on Monday, follow me so you won't miss it!)
Latest comments (48)
A bit late to the party...
Welcome to the party!
Very nice! I feel like this is actually pretty similar to my own Object-oriented approach (if you wanted to read):
Unconditional Fizzbuzz: an Object-oriented approach
Nathan Kallman ・ Jul 6 '20 ・ 3 min read
Has someone done this yet? It's really very fast.
There's a ternary there, I admit.
Nice! But
testuses a ternary?:(which is just a different way to spellifin my opinion)If you can get rid of that I think you'll have a solution!
I can't bring myself to be terribly interested in re-implementing language features like boolean or ternary. I loved seeing some of the really outlandish solutions like that un-sane RegEx that was posted. I enjoyed seeing the contortions done to avoid conditionals, too, but that's not something I'm into for it's own sake. I'm happy to fail at it.
There is a meaningful distinction between ternary and
if.ifis a wilderness with few boundaries, as a form of Many-Valued Logic with an arbitrary number of values. Ternary is a specific subset of Many-Valued Logic; Three-Valued Logic. The Three-Valued Logic of ternary operators provides specific limitations that are not necessarily implied by the N-Valued Logic of theifstatement. In most programming languages ternary is an operator, andifis a control structure block. Operators can be used in expressions, which is generally not true ofifstatements. Ternary is usually slightly computationally more expensive thanifbut has some advantages in terms of limiting complexity.In real-world code I'm going to write an
if(withoutelse iforelseif I can help it) about 95% of the time. It is both more readable and more performant in most cases. But when I need an expression that can make a decision between boolean and a maybe, I reach for ternary.If you wanted to argue that
switchis basicallygotobut potentially worse due to unintended fallthrough, then we'd have something to agree on.The magic of the fizzbuzz problem is, of course, that it helps us understand in more granular detail that our design decisions, and design non-decisions, come with unavoidable trade-offs. It is a meditation on (a horrible term)
non-functional requirements.How about this:
Nice!
This could probably get simplified (I've seen the one-liner of this one), but I still wanted to post mine
I am posting for a friend who found some absolutely mind blowing ideas.
First a one-liner that extends my destructuring idea.
secondly a solution built around a regex from hell/heaven
holy COW did I find out a lot of things about object destructuring while making this. Thanks for the challenge, Nathan. It was so great to notice how instinctively I reach for a conditional.
I'm glad you liked it! I think its good to do a little self reflection on the way we do things every once in a while.
And WOW is that an impressive use of destructuring in JS. I knew about each of those things independently; I never have thought about putting them all together like that!
I'm thinking of an absolutely egregious version using try and catch, but I'm going to go ahead and assume that try/catch would be a "surrogate conditional"
😂 it probably is, but now I want to see it implemented with just try/catch!
Well done! Clever use of
Math.signto select either the first or second element from an arraySlightly shorter without bothering to call out for variable potential conditions and using anons:
Some comments may only be visible to logged-in visitors. Sign in to view all comments.