In this third part we will start converting Hindu-Arabic numbers to Roman numbers.
Convert numbers from 1 to 5
Let’s start by creating a brand new describe section in our test file:
1
2
describe('Check values', () => {
});
We will check the first 5 numbers: 1, 2, 3, 4 and 5.
For any number we want to check both the value returned by toInt (Hindu-Arabic number) and the one returned by toString (Roman Number). For number 1, we will test also its string form ‘1’. Let’s insert our six test cases inside our new describe section:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
describe('Check values', () => {
it('The arabic number 1 should be equal to the Roman number "I"', () => {
let romannum = RomanNumber(1);
romannum.toString().should.equal('I');
romannum.toInt().should.equal(1);
});
it('The arabic number "1" should be equal to the Roman number "I"', () => {
let romannum = RomanNumber('1');
romannum.toString().should.equal('I');
romannum.toInt().should.equal(1);
});
it('The arabic number 2 should be equal to the Roman number "II"', () => {
let romannum = RomanNumber(2);
romannum.toString().should.equal('II');
romannum.toInt().should.equal(2);
});
it('The arabic number 3 should be equal to the Roman number "III"', () => {
let romannum = RomanNumber(3);
romannum.toString().should.equal('III');
romannum.toInt().should.equal(3);
});
it('The arabic number 4 should be equal to the Roman number "IV"', () => {
let romannum = RomanNumber(4);
romannum.toString().should.equal('IV');
romannum.toInt().should.equal(4);
});
it('The arabic number 5 should be equal to the Roman number "V"', () => {
let romannum = RomanNumber(5);
romannum.toString().should.equal('V');
romannum.toInt().should.equal(5);
});
});
We need a big refactor in our code. We need to add two static methods to check:
if a Hindu-Arabic number is valid => isValidInt
if a string contains only Roman symbols => checkOnlyRomanSymbols
This function computes ones and tens values from the input, and then it handles them separately to get the appropriate Roman symbols (See how the case 0 is never handled because we have no Roman symbol representing it).
Now let’s just replace the isValidInt code block in the library constructor
Now let’s check what we accomplished so far, by typing: $ npm test
Everything works :)
Refactor intToRoman
Even though our intToRoman method performs correctly, it really needs refactoring (for style’s sake).
The idea is to group the Roman symbols, based on the ones used for ones, tens, hundreds and thousands. In fact, the pattern used is always the same: it’s just the symbols that change.
Here is the final version of our conversion method intToRoman:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* static method intToRoman
* @param {Integer} val: must be an integer between 1 and 3999 (even in the form '1' to '3999')
*
* The patterns for ones, tens, hundreds and thousands are the same:
* only sumbols change:
*
* Patterns:
*
* | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* Ones: | I | II | III | IV | V | VI | VII | VIII | IX |
*
* | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 |
* Tens: | X | XX | XXX | XL | L | LX | LXX | LXXX | XC |