Posted on • Edited on • Originally published atkallmanation.com
Unconditional Challenge: FizzBuzz without `if`
As the title says, make a classic FizzBuzz function or method without usingif/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
3
and5
, 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) ornull
coalescing operators like??
ornull
safe operators like?.
or&.
.
Also no looping constructs that could be abused into a conditional likewhile
orfor
.
Hint number 1
I taggedfunctional
on this post because functional programming can be used to solve this.
Hint number 2
I taggedoop
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 myWith Only CSS series, styling radio buttons, that's coming on Monday,follow me so you won't miss it!)
Top comments(48)

- Pronounshe/him
- Joined
I'm sure I could make a fancy function composition but my brain is satisfied with this.
constTroo=(iff,elz)=>iff;constFalz=(iff,elz)=>elz;constIf=(boolz,iff,elz)=>boolz(iff,elz);constBoolz={from_bool:bool=>[Falz,Troo][Number(bool)],};constis_fizz=n=>Boolz.from_bool(n%3===0);constis_buzz=n=>Boolz.from_bool(n%5===0);constfizzbuzz=n=>If(is_fizz(n),If(is_buzz(n),"FizzBuzz","Fizz"),If(is_buzz(n),"Buzz",n));constrange=(end)=>Array.from({length:end},(val,index)=>index+1);console.log(range(15).map(fizzbuzz).join(","));

- LocationMontreal
- WorkIntermediate Front End Developer at Diff Agency
- Joined
this blew me away, thank you for posting it

- LocationMontreal
- WorkIntermediate Front End Developer at Diff Agency
- Joined
Aha! She has several functional programming in JavaScript videos I've been putting off for a while, I think this is my cue to watch one. I'd like to better understand your solution. Cheers!

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Awesome! I think you're really getting at the heart of the prompt by definingtrue
andfalse
as functions.

Does this one count for the hard mode?
constfizzBuzz=n=>{constmapper=(arr,modulo,txt)=>arr.filter(e=>e%modulo==0).forEach(e=>arr[arr.indexOf(e)]=txt);letx=1;constrange=[...Array(n)].map(_=>x++)mapper(range,15,'FizzBuzz');mapper(range,5,'Buzz');mapper(range,3,'Fizz');returnrange.toString();}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Nice solution! For hard mode, I think either do it without.filter
or show how.filter
could be implemented according to the rules.

How about with for loops?
constfizzBuzz=n=>{letx=1;constrange=[...Array(n)].map(_=>x++);for(leti=2;i<=n;i+=3)range[i]='Fizz';for(leti=4;i<=n;i+=5)range[i]='Buzz';for(leti=14;i<=n;i+=15)range[i]='FizzBuzz';returnrange.toString();}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Nice! I'll accept it even though I said nofor
loops because I like that you used the stepping statement to simply go through the multiples of 3,5,15. That's creative!
I was more expecting someone to abuse thei <= n
into then % 3
type of check in a traditional fizz buzz. (especially abusingwhile(n % 3)
into a more convolutedif
)

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Happy Juneteenth! I love everyone's approaches here, it's fun to see the way each person thinks through the problem.
I'll be posting my answers on Monday (along with the next installment ofWith Only CSS) so encourage your friends to give this a shot over the weekend (or take another go yourself)!
Follow me and react with a 🔖 so you remember to come back and see what I post here.
Drop a 🦄 or ❤️ if you'd be interested in some follow up posts going more in depth into each (OO/FP) solution: how they work; how they are similar; and how they are different.
Thanks everyone who took the time to post a solution to my little challenge; I'll see you all on Monday!

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
And here is my object-oriented approach (deeper article on it to come soon)
constbaseBoolean={setThen:function(then){return{...this,then};},setOtherwise:function(otherwise){return{...this,otherwise};},};constobjectOrientedTrue={...baseBoolean,evaluate:function(){returnthis.then;},};constobjectOrientedFalse={...baseBoolean,evaluate:function(){returnthis.otherwise;},};constobjectOrientedNumber={value:0,isaMultipleCache:[objectOrientedFalse],setValue:function(n){return{...this,value:n,isaMultipleCache:[objectOrientedTrue,...Array(n).fill(objectOrientedFalse)]};},isaMultipleOf:function(dividend){returnthis.isaMultipleCache[dividend.value%this.value];}};constobjectOrientedFizzBuzz={for:function(n){constnumber=objectOrientedNumber.setValue(n);returnthis.three.isaMultipleOf(number).setThen(this.five.isaMultipleOf(number).setThen("FizzBuzz").setOtherwise("Fizz").evaluate()).setOtherwise(this.five.isaMultipleOf(number).setThen("Buzz").setOtherwise(number.value).evaluate()).evaluate();},three:objectOrientedNumber.setValue(3),five:objectOrientedNumber.setValue(5),};
Similarly to the functional approach; the main part of this solution is defining "true" and "false" as objects. But now instead of parameters, the objects have the same interface and return one or the other of the attributes set on the object.

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
As promised, here is my functional approach to solving this (deeper article on it to come soon)
constfunctionalTrue=(onTrue,onFalse)=>onTrue;constfunctionalFalse=(onTrue,onFalse)=>onFalse;constisDivisible=(dividend,divisor)=>[functionalTrue,...Array(divisor).fill(functionalFalse)][dividend%divisor];constfunctionalFizzBuzz=(n)=>{constdivisible_by_three=isDivisible(n,3);constdivisible_by_five=isDivisible(n,5);returndivisible_by_three(divisible_by_five("FizzBuzz","Fizz"),divisible_by_five("Buzz",n));};
Similar to Heiker's solution above; the main part of this solution is defining "true" and "false" as functions taking the same two parameters but returning one or the other.

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
You can see both solutions in action on this codepen (planning one final post doing a compare/contrast between the two approaches)

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
I think your.get
is a secret conditional. (It will return the value at the givenkey
or the defaultIF the value is undefined)
If you can show how to implement.get
without anif
then I think this is a great answer!

- LocationRotterdam
- WorkFreelance software architect and developer
- Joined
Do you consider something like this cheating, because it builds onfilter
, a built-in, to handle the conditional logic?
constisFizz=n=>n%3===0constisBuzz=n=>n%5===0constcomplement=f=>x=>!(f(x))constfizzbuzz=n=>{constfizz=[n].filter(isFizz).map(_=>'Fizz')constbuzz=[n].filter(isBuzz).map(_=>'Buzz')constnum=[n].filter(complement(isFizz)).filter(complement(isBuzz)).map(String)return`${fizz.join('')}${buzz.join('')}${num.join('')}`}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Clever! I'll accept it for the normal challenge :)
If you want hard mode though, I think you'll have to show an implementation of.filter
that doesn't useif
,?:
,||
,&&
,?.
, or??
. (I'll allow.reduce
to be used though, as well as.map
)
Well done, thanks for commenting!

- LocationRotterdam
- WorkFreelance software architect and developer
- Joined
Okay, well, my next solution is probably not what you were expecting. It builds on the idea that a boolean can be used as a number. I think it meets the hardcore requirements.
constisFizz=n=>n%3===0constisBuzz=n=>n%5===0constisNeither=n=>!isFizz(n)&&!isBuzz(n)// the magic:constoptional=(f,x)=>Array(Number(f(x))).fill(x)constfizzbuzz=n=>{constfizz=optional(isFizz,n).map(_=>'Fizz')constbuzz=optional(isBuzz,n).map(_=>'Buzz')constnum=optional(isNeither,n).map(String)return`${fizz.join('')}${buzz.join('')}${num.join('')}`}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Very nice! I think you have met the hardcore requirements.
And I think slightly less code than my functional solution will be, well done!

functionmap(compare,say,next=v=>v){returnfunction(value){return[()=>say,next][Math.sign(compare(value))](value)}}constprocess=map(v=>(v%3)+(v%5),"fizzbuzz",map(v=>v%5,"buzz",map(v=>v%3,"fizz")))for(leti=1;i<31;i++){console.log(process(i))}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Well done! Clever use ofMath.sign
to select either the first or second element from an array

Slightly shorter without bothering to call out for variable potential conditions and using anons:
letmap=(compare,say,next=v=>v)=>v=>[()=>say,next][Math.sign(v%compare)](v)constprocess=map(15,"FizzBuzz",map(5,"Buzz",map(3,"Fizz")))

- LocationIndia
- EducationBachelor of Computer Science & Engineering
- Joined
Thanks for the problem. I guess this will not satisfy your conditions, but here is my solution,
constfizzbuzz=n=>(!(n%15)&&"FizzBuzz")||(!(n%5)&&"Buzz")||(!(n%3)&&"Fizz")||n

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
It's a nice answer! It satisfies the normal rules (noif
or?:
)
Thanks for taking the time to submit a solution!

- LocationIndia
- EducationBachelor of Computer Science & Engineering
- Joined
Thanks for the reply. I think re-submission is also allowed. So, here is a C++ solution, which again, may not satisfy all your conditions.
stringfizzbuzz(intn){stringnum_string=to_string(n);stringall_options[5][3]={{"FizzBuzz","Buzz","Buzz"},{"Fizz",num_string,num_string},{"Fizz",num_string,num_string},{"Fizz",num_string,num_string},{"Fizz",num_string,num_string}};returnall_options[n%5][n%3];}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Well done! I think you've got a solution

Still not perfect, but this one comes with a special thanks to Mr.Kevlin Henney
constfizzBuzz=n=>{constisFizzBuzz=n=>({false:'',true:'Fizz'}[n%3==0]+{false:'',true:'Buzz'}[n%5==0]||n.toString());letx=1;return[...Array(n)].map(_=>isFizzBuzz(x++)).toString();}

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
Nice! I like how it usesfalse
andtrue
as keys on an object to select the resulting string.
If you can get rid of the||
usage, then this will meet the hard mode requirements...

- LocationMontreal
- WorkIntermediate Front End Developer at Diff Agency
- Joined
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.

- LocationColumbus, OH
- EducationB.S. Computer Engineering
- WorkEngineering Manager at Root Insurance
- Joined
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!
Some comments may only be visible to logged-in visitors.Sign in to view all comments.
For further actions, you may consider blocking this person and/orreporting abuse