A silly bug
===========


FizzBuzz
--------

Rules
+++++

Count from 1 to 100

- If divisible by 3, print "Fizz"
- If divisible by 5, print "Buzz"
- If divisible by 15, print "FizzBuzz"
- Otherwise, print the number

First implementation
+++++++++++++++++++++

.. literalinclude:: /snippets/fizzbuzz-buggy.ts
    :end-before: const main



Quick! Where is the bug?


Well, let's write a `main` and run it:

.. literalinclude:: /snippets/fizzbuzz-buggy.ts
    :start-at: const main

This gives:

.. code-block:: text

    1
    2
    fizz
    4
    buzz
    fizz
    7
    8
    fizz
    buzz
    11
    fizz
    13
    14
    fizz
    16
    17
    fizz
    19
    buzz
    fizz
    22
    23
    fizz
    buzz
    26
    fizz
    28
    29
    fizz

Surely, something is wrong : we never see "fizzbuzz" in the output !


Fixing the bug
---------------

By reading the code carefully, we can see that the bug occurs if the number is divisible by both 3 and 5.

We get through the first `if` first, and return early.

There are many ways to fix the code, one solution is to move the ``if (number % 15 === 0)`` first:


.. literalinclude:: /snippets/fizzbuzz.ts

Writing some tests
------------------

Writing a ``main`` and checking the output by hand is not fun, so let's write some tests instead:

.. literalinclude:: /snippets/fizzbuzz_test.ts


Running the tests
-----------------

.. code-block:: console

    $ deno test fizzbuzz_test.ts

    Check fizzbuzz_test.ts
    running 4 tests from ./fizzbuzz_test.ts
    Print 'fizz' when divisible by 3 ... ok
    Print 'buzz' when divisible by 5 ... ok
    Print 'fizzbuzz' when divisible by 15 ... ok
    Print number otherwise ... ok

    ok | 4 passed | 0 failed (4ms)
