4th Homework
Goal of this this homework is to implement partial FizzBuzz using TDD (Test Driven Development).
goal is to practice TDD using Unit Tests and Jest
goal is not implement fully working FizzBuzz (only partial is good enough)
see below for more info
Deadline: Wednesday 16/12/2020 - 23:59:59
If you have any problems contact your teacher using MS Teams: Tomas Horacek (@hort19).
Specification for FizzBuzz:
fizzBuzz
is a function:- it takes number as an argument
- it returns value based on provided number
fizzBuzz
follows these rules:- if the number is divisible by 3 it returns
"Fizz"
, - if the number is divisible by 5 it returns
"Buzz"
, - if the number is divisible by 3 and 5 (e.g. 15) it returns
"FizzBuzz"
, - else, it returns the number.
- if the number is divisible by 3 it returns
Example inputs and return values:
for const x = ... | fizzBuzz(x) returns: |
---|---|
1 | 1 |
2 | 2 |
3 | "Fizz" |
4 | 4 |
5 | "Buzz" |
6 | "Fizz" |
7 | 7 |
8 | 8 |
9 | "Fizz" |
10 | "Buzz" |
11 | 11 |
12 | "Fizz" |
- more about FizzBuzz on Wikipedia
Sidenote for JavaScript:
- you can determine if a number is divisible by
3
using:
(it works with 5
too 😉)
Unit Tests
Start by crating following file: frontend/src/utils/__tests__/fizzbuzz.js
With this content:
Run yarn test
to start TDD cycle.
Homework Goals
edit
frontend/src/utils/__tests__/fizzbuzz.js
fix failing test (
expect(fizzBuzz(3)).toBe('Fizz')
)
by implementing functionality infunction fizzBuzz(x) { ... }
- remember to use small steps
- implement only what tests will "drive" you to do
step by step write at least:
- two tests for
"Fizz"
(rule 1.), - two tests for
"Buzz"
(rule 2.), - and two tests for "it returns the number" (rule 4.)
- (no need for rule 3., but if you like you can do it too 😉)
- two tests for
use TDD cycle:
- write new failing test
- fix the test
- as fast as possible
- don't be afraid to use "stupid" implementations,
- you can refactor later, when tests are passing
- refactor
- repeat
final implementation of
fizzBuzz
should work correctly for numbers
from1
to12
you can see example of TDD Unit Tests for Game of Life here:
Send the fizzbuzz.js
file
Once you have finished (at least two tests for each rule 1., 2. and 4.; and works correctly for numbers from 1
to 12
)
- send a content of
fizzbuzz.js
file using MS Teams chat to:
Tomas Horacek (@hort19)- it should include
fizzBuzz
function and Unit Tests
- it should include
Deadline: Wednesday 16/12/2020 - 23:59:59