This challenge is intended for Javascript, but you can complete it with any language you like and can.
Most of us will know the FizzBuzz game/exercise and probably have done it many times. It should be a simple and straightforward exercise for most developers...
BUT can you do it without using if/else
statements?
Challenge Description
Write a program that outputs the string representation of numbers from 1 to N.
But for multiples of 3, it should output "Fizz" instead of the number and for the multiples of 5 output "Buzz". For numbers which are multiples of both 3 and 5, you should output "FizzBuzz".
Curveball: You must not use if/else statements, and ideally, no ternary operator.
Example:
const n = 15;
/*
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
*/
I will comment my solution in a couple of days.
💪 Best of luck! 💪
Credits:
Cover Image from https://codenewbiesite.wordpress.com/2017/01/29/fizz-buzz/
Latest comments (93)
Pretty interesting cheers! I recall using it on the CLI at some point, but without actually knowing it was a language!
That's pretty neat! It reminds my a bit of rust's match. What was/is awk usually used for?
Nice, I have heard about it but never saw any writen code for awk!!
Bit late to the party...
A bit late to the party but here is my take. I'm basically substituting
cond ? valueWhenTrue : valueWhenFalse
for a logical equivalent(cond && valueWhenTrue) || valueWhenFalse
+ calling the function recursively.Note: array returned is in reverse order ! But we can easily solve that with
.reverse()
Here's my proposal for a non if/else implementation
Using only Types and Abstractions (Interface) and a bit a Linq (C#)
But the most important part is using full polymorphism and a DDD style with immutable objects (structs)
Made with strict TDD (all tests green), please check my repo
My 10 cents:
you should have said: FizzBuzz with a Cyclomatic Complexity to maximum 2 (or 1).
Because if you replace a if/else by Logical Operators, or Switch or While or For, you aren't doing better code in terms of Cyclomatic Complexity
The CSS approach is one of the most elegant.
Another one without any cyclomatic complexity is acheived with a strong type system, like what you can do in Haskell, F#; Kotlin or TypeScript
See that solution (not mine, but clever if you understand the type inference mecanism) gist.github.com/na-o-ys/34e9ebf7fc...
Really nice idea!
Here's how I did it 😁
Glad you liked it!
I just published a new challenge if you want another one :)
[Challenge] log("this") or log("this").withData({})
Manolo Edge ・ Aug 19 ・ 1 min read
One of the ways in Clojure:
Original idea seen here: clojuredocs.org/clojure.core/cond-...
Manolo Edge
@nombrekeff
My approach is to take advantage of string replace 🤣️
Smart!
Here you have in Python
Maybe we could have it ready from the DB. I know CASE / WHEN / THEM is sql if / else equivalent, but nobody tried yet, so here he go
Also thinking about overriding Number.prototype.toString makes a fun thingy. Maybe someone already did, but someone for sure should :D
Some improvement to my earlier version.
A) better (arguably, because way more cognitive load than the very simple one above)
B) above one as 1 liner b/c hello perl
Very stupid solution ! but this is the first thing that comes to my mind !