-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromanConverter.js
131 lines (126 loc) · 3.12 KB
/
romanConverter.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
123
124
125
126
127
128
129
130
131
export const convertToRoman = (num) => {
let numToString; //the variable that will hold the value of the num passed
//if the num it's type of number, turn it to a string, else, it'll already be a string
if (typeof num == "number"){
numToString = String(num)
}
else {
numToString = num;
}
const stringNum = [];
//save each individual number into an array, starting from rigth to left (or end to start)
for (let i = numToString.length - 1; i >= 0; i--){
stringNum.push(numToString[i]);
}
//iterate trough the array and depending on the position of the arr and wich number it is, convert it to its equivalent in roman numeral
for (let i = 0; i < stringNum.length; i++){
if (stringNum[i] == 0){
stringNum[i] = "";
}
switch (stringNum[0]){
case "1":
stringNum[0] = "I"
break;
case "2":
stringNum[0] = "II"
break;
case "3":
stringNum[0] = "III"
break;
case "4":
stringNum[0] = "IV"
break;
case "5":
stringNum[0] = "V"
break;
case "6":
stringNum[0] = "VI"
break;
case "7":
stringNum[0] = "VII"
break;
case "8":
stringNum[0] = "VIII"
break;
case "9":
stringNum[0] = "IX"
break;
}
switch (stringNum[1]){
case "1":
stringNum[1] = "X"
break;
case "2":
stringNum[1] = "XX"
break;
case "3":
stringNum[1] = "XXX"
break;
case "4":
stringNum[1] = "XL"
break;
case "5":
stringNum[1] = "L"
break;
case "6":
stringNum[1] = "LX"
break;
case "7":
stringNum[1] = "LXX"
break;
case "8":
stringNum[1] = "LXXX"
break;
case "9":
stringNum[1] = "XC"
break;
}
switch (stringNum[2]){
case "1":
stringNum[2] = "C"
break;
case "2":
stringNum[2] = "CC"
break;
case "3":
stringNum[2] = "CCC"
break;
case "4":
stringNum[2] = "CD"
break;
case "5":
stringNum[2] = "D"
break;
case "6":
stringNum[2] = "DC"
break;
case "7":
stringNum[2] = "DCC"
break;
case "8":
stringNum[2] = "DCCC"
break;
case "9":
stringNum[2] = "CM"
break;
}
switch (stringNum[3]){
case "1":
stringNum[3] = "M"
break;
case "2":
stringNum[3] = "MM"
break;
case "3":
stringNum[3] = "MMM"
break;
}
}
//once its done, pass the convert numbers to a new array, from end to start
let romanNum = [];
for (let i = stringNum.length - 1; i >= 0; i--){
romanNum.push(stringNum[i]);
}
//use the .join to return the new string
return romanNum.join("");
}