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/
Oldest comments (93)
I did that in scheme r5rs:
As promised, no ifs.
Thanks for sharing Tanguy!
Cool stuff! Haven't heard about r5rs, looks complicated, but cool :P
Oh it isn’t that hard, it might seem complicated because of all the parenthesis and strange keywords like cons, but it’s actually just a matter of getting used to it. It’s a fun language to learn recursion for example.
It becomes a lot simpler when you learn the patterns for building recursive functions, at one point, you don’t even look at the whole function because you just think of the parts.
I started by making a function that looks like that:
This function just returns the list of numbers from 1 to 15, that's what it does because the
compute
function just returns whatever isn
. Then I simply filledcompute
with my conditions.define
is used to define variables and functions,cons
is used to build a list from a head and a queue (that's not hard to understand but it definetely requires some practice and research,let*
allows to define local variablesWhat a legend! I wasn't expecting an explanation, but it's highly appreciated!
Looks interesting, and after your explanation, it does not look as complicated, I might give it a try someday💪
And yeah, most languages seem a bit overwhelming at first.
Thanks! Feel free to ping me for help if you try to get into it and feel stuck.
Thank you! I will if I get into it :)
I don't know this language, but the
(and (zero? ...
sounds like ternaries to me :)It’s not a ternary, it’s just like doing && in other languages. Now you can emulate a ternary with it, I think that was the goal of the challenge. No ifs and no ternary.
Thanks for the fun challenge!
I'm not sure if JavaScript's type conversion is considered cheating, but I thought it was cool and wanted to share!
why
_value
?I understand that the 'convention' for
_
is for private, but is there some other use for it here?Or is it just habit 😂
It is also a convention for unused parameters.
Thanks for sharing Jesse!! It's a really neat solution 🤘!
Also not cheating at all!
Hmm... As a bootcamp student, I'm trying to untangle this.
[!(offsetIndex % 3) + 0]
I see this checks the modulus, and inverts the result. Any non-zero int is truthy, and this expression makes it false . . .
+0
to coerce thefalse
to an int. That is enough that the entire thing evaluates falsy, which then results in outputtingoffsetIndex
on the otherside of theor
. I had to drop this in a node repl to follow it, but I eventually got it 😁But what is the
["", "Fizz"][!(offsetIndex % 3) + 0]
double-array looking thing there? I thought it was a 2d array at first, but that doesn't seem right for a number of reasons.I'm pretty sure the first pair of square brackets creates an array, and the second one indexes into that array. So I think they are array indexing into the first array with either 0 or 1 to pick the empty string or "Fizz" depending on the offsetIndex!
Hope that helps!
yup, it defines the array first
const array = ["", "Fizz"]
and then access the indexarray[!(offsetIndex % 3) + 0]
. The expression will resolve either totrue+0 -> 1
orfalse+0 -> 0
holy shit. that's cool.
I THOUGHT it might have been something like that, but I was thinking about it wrongly . . . I wasn't thinking of it as the array followed by the index, I was thinking of it as a variable. So
["an", "array"]
was the name of the array, and then it had it's own index. Not very practical.But the actual use is quite cool and makes plenty sense.
Thanks!
Thanks for the fun challenge! My solution uses bitwise operators and objects to get the answer:
Neat!
Here's a solution to the challenge in JavaScript...
Here's a repl.it where you can run this.
A solution in Python. Feels silly though...
Wait until you see mine 😂
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.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/
I did not know about this, thanks for sharing. I will remember this!
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
You can still have flow control with functions.
I liked this approach! Thanks for sharing!
I do feel like this was kinda cheap...
I like it!
Nice stuff! Cool use of a generator. And yeah kinda cheap but cool nonetheless, thanks for sharing!
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
Cool solution! I thought to do something similar at first, but ended up doing some weird stuff!
Here is some python for you :)
Here's the simplest I can think of without any statements. 🙃
Nice, recursion for the win 💪
Thanks for sharing!
fun challenge! this is the simplest thing I can think of at the moment