DEV Community

Discussion on: Unconditional Challenge: FizzBuzz without `if`

Collapse
 
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(
  is_fizz(n),
  If(is_buzz(n), "FizzBuzz", "Fizz"),
  If(is_buzz(n), "Buzz", n)
);

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

console.log(
  range(15)
    .map(fizzbuzz)
    .join(", ")
);
Collapse
 
kallmanation profile image
Nathan Kallman

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

Collapse
 
bashunaimiroy profile image
Bashu Naimi-Roy

this blew me away, thank you for posting it

Collapse
 
vonheikemen profile image
Heiker

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!