2 ms·
I don’t want to argue in favour of the Beads solution but I think the right way to describe the logic is as follows: input is n | 3 divides n | 5 divides n
by dan-robertson 5y ago
I don’t want to argue in favour of the Beads solution but I think the right way to describe the logic is as follows:
input is n
| 3 divides n | 5 divides n | result |
|—————————————|—————————————|————————————|
| Yes | Yes | “FizzBuzz” |
| Yes | No | “Fizz” |
| No | Yes | “Buzz” |
| No | No | n.toString |
print result
The key points are:
1. The logic is declarative: the order of the rows in the table doesn’t matter.
2. It tests for divisibility in words rather than a potentially confusing idiom (how does % behave for negative numbers? Is it defined for floats in this language?)
3. The structure allows you (or a compiler) to check that no case is missed.
4. It should be easier to convince oneself that modifications to the code do what they are supposed to.
The funny thing is that looking things up in tables is much more common in excel.
- jacksnipe 5y agoI agree, I think that's close to isomorphic to my first solution! There's a "mapping" from numbers to strings, and this is the mapping.