-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (29 loc) · 877 Bytes
/
index.js
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
const translations = [
{ decimal: 1000, roman: 'M' },
{ decimal: 900, roman: 'CM' },
{ decimal: 500, roman: 'D' },
{ decimal: 400, roman: 'CD' },
{ decimal: 100, roman: 'C' },
{ decimal: 90, roman: 'XC' },
{ decimal: 50, roman: 'L' },
{ decimal: 40, roman: 'XL' },
{ decimal: 10, roman: 'X' },
{ decimal: 9, roman: 'IX' },
{ decimal: 5, roman: 'V' },
{ decimal: 4, roman: 'IV' },
{ decimal: 1, roman: 'I' },
];
module.exports = (roman) => {
const regex = /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/;
if (!regex.test(roman)) {
throw new TypeError('not valid numeral');
}
let decimal = 0;
for (let i = 0; i < translations.length; i++) {
while (roman.indexOf(translations[i].roman) === 0) {
decimal += translations[i].decimal;
roman = roman.replace(translations[i].roman, '');
}
}
return decimal;
}