-
Notifications
You must be signed in to change notification settings - Fork 0
/
arabicToRomanNumbers1000.js
110 lines (100 loc) · 2.07 KB
/
arabicToRomanNumbers1000.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Convert Arabic numbers (upto 1000) into Roman numerals
function romVal(x){
let xRom = [];
let y1 = '';
let y2 = '';
let y3 = '';
if(x[0] == 0){
xRom.push('');
}else{
xRom.push('M');
}
if(x[1] == 0){
xRom.push('');
}else if(x[1] > 0 && x[1] < 4){
while(y1.length < x[1]){
y1 = y1 + 'C';
}
xRom.push(y1);
}else if(x[1] == 4){
y1 = 'CD'; xRom.push(y1);
}else if(x[1] == 5){
y1 = 'D';
xRom.push(y1);
}else if(x[1] > 5 && x[1] < 9){
y1 = 'D';
while(y1.length < x[1]-4){
y1 = y1 + 'C';
}xRom.push(y1);
}else{
y1 = 'CM';
xRom.push(y1);
}
if(x[2] == 0){
xRom.push('');
}else if(x[2] > 0 && x[2] < 4){
while(y2.length < x[2]){
y2 = y2 + 'X';
}xRom.push(y2);
}else if(x[2] == 4){
y2 = 'XL';
xRom.push(y2);
}else if(x[2] == 5){
y2 = 'L';
xRom.push(y2);
}else if(x[2] > 5 && x[2] < 9){
y2 = 'L';
while(y2.length < x[2]-4){
y2 = y2 + 'X';
}xRom.push(y2);
}else{
y2 = 'XC';
xRom.push(y2);
}
if(x[3] == 0){
xRom.push('');
}else if(x[3] > 0 && x[3] < 4){
while(y3.length < x[3]){
y3 = y3 + 'I';
}xRom.push(y3);
}else if(x[3] == 4){
y3 = 'IV';
xRom.push(y3);
}else if(x[3] == 5){
y3 = 'V';
xRom.push(y3);
}else if(x[3] > 5 && x[3] < 9){
y3 = 'V';
while(y3.length < x[3]-4){
y3 = y3 + 'I';
}xRom.push(y3);
}else{
y3 = 'IX';
xRom.push(y3);
}
return xRom[0] + xRom[1] + xRom[2] + xRom[3];
}
function roman(x){
let xhsl = [];
if(x == 1000){
x1 = x/1000;
xhsl = [1, 0, 0, 0];
}else if(x>= 100 && x<1000){
xhsl = [0];
xhsl.push(Math.floor(x/100));
xhsl.push(Math.floor((x%100)/10));
xhsl.push((x%100)%10);
}else if(x>=10 && x<100){
xhsl = [0, 0];
xhsl.push(Math.floor(x/10));
xhsl.push(x%10);
}else{
xhsl = [0, 0, 0];
xhsl.push(x);
}
return romVal(xhsl);
}
console.log(roman(241)); // result: "CCXLI"
console.log(roman(561)); // result: "DLXI"
console.log(roman(938)); // result: "CMXXXVIII"
console.log(roman(1000)); // result: "M"