DEV Community

loading...

Discussion on: Unconditional Challenge: FizzBuzz without `if`

Collapse
vidit1999 profile image
Vidit Sarkar

Thanks for the problem. I guess this will not satisfy your conditions, but here is my solution,

const fizzbuzz = n => (!(n%15) && "FizzBuzz") ||
                      (!(n%5) && "Buzz") ||
                      (!(n%3) && "Fizz") ||
                      n
Enter fullscreen mode Exit fullscreen mode
Collapse
kallmanation profile image
Nathan Kallman Author

It's a nice answer! It satisfies the normal rules (no if or ?:)

Thanks for taking the time to submit a solution!

Collapse
vidit1999 profile image
Vidit Sarkar

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.

string fizzbuzz(int n){
    string num_string = to_string(n);

    string all_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}
    };

    return all_options[n%5][n%3];
}
Thread Thread
kallmanation profile image
Nathan Kallman Author

Well done! I think you've got a solution

Forem Open with the Forem app