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/
Top comments (93)
Easy, just do it in CSS.
Needs some Markup to display obviously.
Ha!
Magnificent! I knew there were going to be really neat solutions!!
Thanks for sharing your solution ๐ช
The cleanest and best FizzBuzz implementation I know of doesn't use any if statements at all. Actually it doesn't use any control flow at all in most languages.
The approach is fully described here: philcrissman.net/posts/eulers-fizz...
On my Repl.it I also have this same approach implemented in several other languages:
repl.it/@rushsteve1/
This was great! Love it when there is a simple probable mathematic solution to these kinds of things!
Me too, so clean! I love maths but I'm crap at it myself xD
I did not know about this, thanks for sharing. I will remember this!
Holy sh*t, my other solution was really ugly! :-o
Here is a (much) better one (also in Python3):
This works using the property that
True
in Python has numerical value1
and using f-strings in an array. The proper element in the array is chosen based on the mentioned property, checking for divisibility with 3 and 5.logical operators
The second half of an "and" statement only evaluates if the first half is true.
...
for loops
For loops check your second declaration on each iteration. Force it to be false on the second iteration, and you've got something that'll check your condition a single time.
...
arrays
Referencing an index that that exists gives you the value stored there, but referencing an index that doesn't exist gives you undefined. Use an "or" statement to give yourself a fallback value when this happens, and you'll be ready to go.
Or, fill an array with your options, and leverage the fact that true can be used as 1 in JavaScript to do some index-selection math.
...
try/catch blocks
You can purposefully throw exceptions when a boolean is false by referencing a variable that doesn't exist (the "throwException" variable in this case).
...
while loops
This is the same concept as for loops. Force the loop to stop after one iteration (this time with a break), and you've got something that'll check your condition a single time.
...
switch statements
Who could forget the classic alternative to if statements? Technically not even cheating!
Wow, those are some solutions right there! Thanks a lot for sharing and taking the time to explain it.
I did some silly stuff, just for fun lol:
Here's the simplest I can think of without any statements. ๐
Nice, recursion for the win ๐ช
Thanks for sharing!
You can still have flow control with functions.
I liked this approach! Thanks for sharing!
Here is an ugly solution in one line
U aren't supposed to use Ternary Operator.
Oh yeah! didn't notice that, I have updated my solution
Here is some python for you :)
There was a similar and equally really good thread about a month ago that had some devilishly clever solutions... highly recommend it!
My contributions below:
Nice stuff. I will be checking out the thread!
There have been some really clever solutions posted here as well.
Manolo Edge
@nombrekeff
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
Also thinking about overriding Number.prototype.toString makes a fun thingy. Maybe someone already did, but someone for sure should :D
My javascript solution, modeled after my C++ solution and then reduced
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
Clojure example.
I quite like Andrew's bit shift array example. Only think that its better to have a
nil
zeroth value so you get circuit breaking for free.repl.it link
One of the ways in Clojure:
Original idea seen here: clojuredocs.org/clojure.core/cond-...