-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roman-Numbers.js
122 lines (99 loc) · 2.21 KB
/
Roman-Numbers.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
111
112
113
114
115
116
117
118
119
120
121
122
//Convert the given number into a roman numeral.
function convertToRoman(num) {
var romanNumbers = {
1000 : "M",
900 : "CM",
500 : "D",
100 : "C",
90 : "XC",
50 : "L",
40 : "XL",
10 : "X",
9 : "IX",
5 : "V",
4 : "IV",
1 : "I"
};
var romanNumber = "";
if(num > 999){
var numThousands = String(num).split('');
var thousands = Number(numThousands[0]);
if (thousands == 1){
romanNumber = romanNumber + romanNumbers[1000];
num -= 1000;
} else {
for(var i = 1; i <= thousands; i++ ){
romanNumber = romanNumber + romanNumbers[1000];
num -= 1000;
}
}
}
if(num >= 100 && num <= 999){
var numHundreds = String(num).split('');
var hundreds = Number(numHundreds[0]);
if (hundreds == 9){
romanNumber = romanNumber + romanNumbers[900];
hundreds -= 9;
num -= 900;
}
if (hundreds >= 5){
romanNumber = romanNumber + romanNumbers[500];
hundreds -= 5;
num -= 500;
}
for(var j = 1; j <= hundreds; j++ ){
romanNumber = romanNumber + romanNumbers[100];
num -= 100;
}
}
if(num >= 10 && num <= 99){
var numDeceneas = String(num).split('');
var decenas = Number(numDeceneas[0]);
if (decenas == 9){
romanNumber = romanNumber + romanNumbers[90];
decenas -= 9;
num -= 90;
}
if (decenas >= 5){
romanNumber = romanNumber + romanNumbers[50];
decenas -= 5;
num -= 50;
}
if (decenas == 4){
romanNumber = romanNumber + romanNumbers[40];
decenas -= 4;
num -= 40;
}
for(var k = 1; k <= decenas; k++ ){
romanNumber = romanNumber + romanNumbers[10];
num -= 10;
}
}
if(num >= 1 && num <= 9){
var numSingles = String(num).split('');
var singles = Number(numSingles[0]);
if (singles == 9){
romanNumber = romanNumber + romanNumbers[9];
singles -= 9;
num -= 9;
}
if (singles >= 5){
romanNumber = romanNumber + romanNumbers[5];
singles -= 5;
num -= 5;
}
if (singles == 4){
romanNumber = romanNumber + romanNumbers[4];
singles -= 4;
num -= 4;
}
for(var m = 1; m <= singles; m++ ){
romanNumber = romanNumber + romanNumbers[1];
num--;
}
}
console.log(num);
console.log(romanNumber);
return romanNumber;
}
convertToRoman(9999);