DEV Community


Discussion on: Unconditional Challenge: FizzBuzz without `if`

bashunaimiroy profile image
Bashu Naimi-Roy • Edited

I am posting for a friend who found some absolutely mind blowing ideas.

First a one-liner that extends my destructuring idea.

const fn = n => ({ ['2xx0x10xx01x0'[n % 15]]: result = n } = ['Fizz', 'Buzz', 'FizzBuzz'], result);
Enter fullscreen mode Exit fullscreen mode

secondly a solution built around a regex from hell/heaven

let fn = n => String(n).replace(/^(?=.*[05]$)(?:[0369]|[147](?:[0369]|[147][0369]*(?:[258]|[147][0369]*[147]))*(?:[258]|[147][0369]*[147])|[258](?:[258]|[258][0369]*(?:[147]|[258][0369]*[258]))*(?:[147]|[258][0369]*[258]))*$/, 'FizzBuzz').replace(/^(?:[0369]|[147](?:[0369]|[147][0369]*(?:[258]|[147][0369]*[147]))*(?:[258]|[147][0369]*[147])|[258](?:[258]|[258][0369]*(?:[147]|[258][0369]*[258]))*(?:[147]|[258][0369]*[258]))*$/, 'Fizz').replace(/^\d*[05]$/, 'Buzz');
Enter fullscreen mode Exit fullscreen mode
Forem Open with the Forem app