DEV Community


Discussion on: Unconditional Challenge: FizzBuzz without `if`

vonheikemen profile image
Heiker • Edited

I'm sure I could make a fancy function composition but my brain is satisfied with this.

const Troo = (iff, elz) => iff;
const Falz = (iff, elz) => elz;

const If  = (boolz, iff, elz) => boolz(iff, elz);

const Boolz = {
  from_bool: bool => [Falz, Troo][Number(bool)],

const is_fizz = n => Boolz.from_bool(n % 3 === 0);
const is_buzz = n => Boolz.from_bool(n % 5 === 0);

const fizzbuzz = n => If(
  If(is_buzz(n), "FizzBuzz", "Fizz"),
  If(is_buzz(n), "Buzz", n)

const range = (end) => Array.from({ length: end }, (val, index) => index + 1);

    .join(", ")
kallmanation profile image
Nathan Kallman Author

Awesome! I think you're really getting at the heart of the prompt by defining true and false as functions.

bashunaimiroy profile image
Bashu Naimi-Roy

this blew me away, thank you for posting it

vonheikemen profile image

Glad you like it. I got the idea from this talk by Anjana Vakil.

Thread Thread
bashunaimiroy profile image
Bashu Naimi-Roy

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!

Forem Open with the Forem app