TDD Code Katas
Clone this repository, navigate to the root of the project, and run
npm install. To run tests whenever a file
gulp tdd in the terminal.
- To run a test once, run
gulp testin the terminal
- To view unit test code coverage details, ruclearn
gulp coveragein the terminal
There are a number of exercises listed below for you to work through. Practice using test-driven development while solving these code katas. Write out the tests before fleshing out your solutions. Remember to use the minimum amount of code necessary to make all the tests pass, refactoring as necessary (Red-Green-Refactor).
For each of the examples, create a function solving the kata in it's own file inside the
Assign it to
module.exports so it can used in your test suite, which should reside in it's own file inside
test/ directory (don't forget the
.spec suffix). Ex:
src/test-name.js gets tested in
Exercise 1 - Prime Factors
Note: This has already been completed in
test/prime-factors.spec.js files. It can be
used as a reference.
Write a function that will return an array of all the prime factors of any positive integer.
Remember that a prime factor is a positive integer that is only divisible by itself and
2is only divisible by itself and
1. The function would return
3is only divisible by itself and
1. The function would return
4is divisible by
4 2 * 2
2 * 2 = 4. The function would return
5is not divisible by
3, but only by itself. The function would return
6is divisible by
6 2 * 3
2 * 3 = 6. The function would return
10is divisible by
10 2 * 5
2 * 5 = 10. The function would return
12is divisible by
6is not a prime number, and is divisible by
12 2 * 6 2 * 3
2 * 2 * 3 = 12. The function would return
[2, 2, 3]
100is divisible by
50is divisible by
25is divisible by
100 2 * 50 2 * 25 5 * 5
2 * 2 * 5 * 5 = 100. The function would return
[2, 2, 5, 5]
Exercise 2 - Palindrome
Write a function that will determine if a string, phrase, or number passed in is a palindrome or not. Remember that a palindrome is a value that reads the same forwards and backgrounds.
- Function returns a boolean
- Function allows for multiple words and numbers
Exercise 3 - FizzBuzz
Write a function that will return
Fizz if an integer entered is divisible by
Buzz if a number entered is
FizzBuzz if a number entered is divisible by
5, and return the number if not divisible
Exercise 4 - Roman Numerals Converter
Write a function that will convert a positive integer between 1 and 4999 into roman numerals.
Exercise 5 - Bowling Game
Write a class that will reproduce a scoring system for a game of bowling. Make two methods:
roll(pins: int)- called each time a player rolls the ball. The argument is the number of pins knocked down
score()- called only at the very end of the game and returns an integer of the total score for that game
Bowling.roll(3); Bowling.roll(5); Bowling.roll(10); ... Bowling.score();
Remember that a game of bowling consists of 10 frames (or rounds). In each frame, a player can take one or two rolls at knocking down the 10 pins, collecting a score of 1 point per pin knocked down. Assuming a player doesn't knock down all the pins on the first roll of the frame, the score for the frame is the number of pins from the first roll plus the number of pins from the second roll. Once all players roll 10 frames, the scores for each player are tallied up and the final score is computed.
- Spares: A spare occurs when a player fails to knock down all of the pins in the first frame, but succeeds in
knocking down the remainder in the second frame. The points of that player's next roll are counted and then also added
onto the score of the frame where the spare occurred.
- Ex: Frame 3, roll 1: 8 points. Frame 3, roll 2: 2 points (spare!). Frame 4, roll 1: 8 points, Frame 4, roll 2: 1 point.
Score for those two frames would be
(10 + 8) + 8 + 1 = 27.
- Ex: Frame 3, roll 1: 8 points. Frame 3, roll 2: 2 points (spare!). Frame 4, roll 1: 8 points, Frame 4, roll 2: 1 point. Score for those two frames would be
- Strikes: A strike happens when a player knocks down all 10 pins in the first (and only) roll of a frame. The next two
rolls are added to the score of the frame where the strike occurred.
- Ex: Frame 3, roll 1: 10 points (strike! no second roll). Frame 4, roll 1: 8 points. Frame 4 roll 2: 1 point. Score for those two
frames would be
(10 + 8 + 1) + 8 + 1 = 28.
- Ex: Frame 3, roll 1: 10 points (strike! no second roll). Frame 4, roll 1: 8 points. Frame 4 roll 2: 1 point. Score for those two frames would be
- In the 10th frame, a player who rolls a spare or a strike may roll the ball extra times to complete the frame, though no more than rolls can occur.
Exercise 6 - String Calculator
Write a function that will accept a string of numbers and return an integer representing the sum of the numbers contained in the string.
stringCalc('2,3,4') would return
- An empty string passed in should be interpreted as
- Either comma or
\ncan be the delimeter
- Numbers greater than 1000 should be ignored
- Numbers less than 0 are not allowed
Exercise 7 - Tennis Scoring
Write a function that translates points to the proper tennis score.
tennisScore(0,2) would return
The first player to win 4 points is the winner, unless the score is 3 points to 3 points. Whenever the points are equal (from 3, upwards), the score is called "deuce." If player one scores a point after "deuce," then it is player 1's advantage (a.k.a. "Advantage in"). If player two scores a point after "deuce," then it is "Advantage out". This can go on indefinitely until one player scores two points in a row.