-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (85 loc) · 1.91 KB
/
app.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
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
84
85
86
87
88
89
90
91
92
93
94
95
const readline = require('readline');
const romanAndDecimal = {
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1,
}
const romanWrongAndRight = {
'IIII': 'IV',
'VIV': 'IX',
'XXXX': 'XL',
'LXL': 'XC',
'CCCC': 'CD',
'DCD': 'CM',
}
function main(){
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '>> ',
});
rl.prompt();
rl.on('line', (line) => {
switch (line.trim()) {
case 'exit':
process.exit(0);
break;
default:
if(line > 3999 || line <= 0) {
console.log(`Only numbers from 1 to 3999`);
break;
}
let ret = toRoman(line);
console.log(ret);
break;
}
rl.prompt();
}).on('close', () => {
process.exit(0);
});
}
// converts decimal to roman number
function toRoman(decimalNumber){
let romanNumber = '';
while (decimalNumber > 0) {
for (let roman in romanAndDecimal) {
let value = romanAndDecimal[roman];
if (!(decimalNumber % value)) {
romanNumber += roman;
decimalNumber -= value;
break;
}
}
}
return replaceBad(romanNumber.split("").reverse().join(""));
}
// checks if roman number is valid
function validateNumber(input){
return /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/
.test(input);
}
// replaces bad roman numbers with the right ones (example: IIII -> IV)
function replaceBad(inputRoman){
while(!validateNumber(inputRoman)){
let outputRoman = '';
for (let wrongRoman in romanWrongAndRight) {
let rightRoman = romanWrongAndRight[wrongRoman];
let regex = new RegExp(wrongRoman);
if (inputRoman.includes(wrongRoman)) {
outputRoman = inputRoman.replace(regex, rightRoman)
}
}
inputRoman = outputRoman;
}
return inputRoman;
}
main();
module.exports = {
toRoman,
validateNumber,
replaceBad,
}