-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman-Numeral-Converter.js
More file actions
72 lines (70 loc) · 1.56 KB
/
Copy pathRoman-Numeral-Converter.js
File metadata and controls
72 lines (70 loc) · 1.56 KB
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
function convertToRoman(num) {
// I=1, V=5, X=10, L=50, C=100, D=500, and M=1000,
let arr=num.toString().split("");
let newArr=[];
let roman=[];
for(let i=0;i<arr.length;i++){
newArr[i]=arr[arr.length-1-i];
}
for(let i=0;i<newArr.length;i++){
//ones digit
switch (parseInt(newArr[i])){
case 0:
break;
case 1:
if(i==0){roman[i]="I";}
if(i==1){roman[i]="X";}
if(i==2){roman[i]="C";}
if(i==3){roman[i]="M";}
break;
case 2:
if(i==0){roman[i]="II";}
if(i==1){roman[i]="XX";}
if(i==2){roman[i]="CC";}
if(i==3){roman[i]="MM";}
break;
case 3:
if(i==0){roman[i]="III";}
if(i==1){roman[i]="XXX";}
if(i==2){roman[i]="CCC";}
if(i==3){roman[i]="MMM";}
break;
case 4:
if(i==0){roman[i]="IV";}
if(i==1){roman[i]="XL";}
if(i==2){roman[i]="CD";}
break;
case 5:
if(i==0){roman[i]="V";}
if(i==1){roman[i]="L";}
if(i==2){roman[i]="D";}
break;
case 6:
if(i==0){roman[i]="VI";}
if(i==1){roman[i]="LX";}
if(i==2){roman[i]="DC";}
break;
case 7:
if(i==0){roman[i]="VII";}
if(i==1){roman[i]="LXX";}
if(i==2){roman[i]="DCC";}
break;
case 8:
if(i==0){roman[i]="VIII";}
if(i==1){roman[i]="LXXX";}
if(i==2){roman[i]="DCCC";}
break;
case 9:
if(i==0){roman[i]="IX";}
if(i==1){roman[i]="XC";}
if(i==2){roman[i]="CM";}
break;
}
}
let newRoman=[];
for(let i=0;i<roman.length;i++){
newRoman[i]=roman[roman.length-1-i];
}
return newRoman.join("");
}
console.log(convertToRoman(1000));