-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoman Numeral Converter.jS
78 lines (75 loc) · 1.47 KB
/
Roman Numeral Converter.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
/*Convert the given number into a roman numeral.*/
function convertToRoman(num) {
const romanNumerals = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M"
}
// return value
let romanNumeral = ""
while (num !== 0) {
if (num >= 1000) {
romanNumeral += romanNumerals[1000];
num -= 1000;
}
else if(num >= 900){
romanNumeral += romanNumerals[900];
num -= 900;
}
else if(num >= 500){
romanNumeral += romanNumerals[500];
num -= 500;
}
else if(num >= 400){
romanNumeral += romanNumerals[400];
num -= 400;
}
else if(num >=100){
romanNumeral += romanNumerals[100];
num -= 100;
}
else if(num >= 90){
romanNumeral += romanNumerals[90];
num -= 90;
}
else if(num >=50){
romanNumeral += romanNumerals[50];
num -= 50;
}
else if(num >= 40){
romanNumeral += romanNumerals[40];
num -= 40;
}
else if(num >=10){
romanNumeral += romanNumerals[10];
num -= 10;
}
else if(num >=9){
romanNumeral += romanNumerals[9];
num -= 9;
}
else if(num >=5){
romanNumeral += romanNumerals[5];
num -= 5;
}
else if(num >= 4){
romanNumeral += romanNumerals[4];
num -= 4;
}
else {
romanNumeral += romanNumerals[1];
num -= 1;
}
}
return romanNumeral
}