DEV Community

loading...

Discussion on: Unconditional Challenge: FizzBuzz without `if`

Collapse
jpantunes profile image
JP Antunes

Still not perfect, but this one comes with a special thanks to Mr. Kevlin Henney

const fizzBuzz = n => {
    const isFizzBuzz = n => ( {false: '', true: 'Fizz'}[n % 3 == 0] 
                            + {false: '', true: 'Buzz'}[n % 5 == 0] 
                            || n.toString() );
    let x = 1;
    return [...Array(n)].map(_ => isFizzBuzz(x++)).toString();                             
}
Enter fullscreen mode Exit fullscreen mode
Collapse
kallmanation profile image
Nathan Kallman Author

Nice! I like how it uses false and true 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...

Collapse
jpantunes profile image
JP Antunes • Edited

Well Mr. Henney has a great answer:

const fizzBuzz = n => {
  const test = (d, s, x) => n % d == 0 ? _ => s + x('') : x;
  const fizz = x => test(3, 'Fizz', x);
  const buzz = x => test(5, 'Buzz', x);

  return fizz(buzz(x => x))(n.toString());
}

edit: but it still has a conditional...

Forem Open with the Forem app