-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanNumeralConverter.js
52 lines (45 loc) · 1.03 KB
/
RomanNumeralConverter.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
function convertToRoman(num) {
//Create array that contains roman values
var romanValues = [
[1000, 'M'],
[900, 'CM'],
[500, 'D'],
[400, 'CD'],
[100, 'C'],
[90, 'XC'],
[50, 'L'],
[40, 'XL'],
[10, 'X'],
[9, 'IX'],
[5, 'V'],
[4, 'IV'],
[1, 'I']
];
//Condition, if the value in num is less than or equal to zero, return an empty string
if (num <= 0) {
return '';
}
/*Else, run a loop through the array created above
*/
for (var i = 0; i < romanValues.length; i++) {
/*Condition, if the value in num is greater or equal to the value
inside array the element in the array:
*/
if (num >= romanValues[i][0]) {
//Return letter for numeric value
return romanValues[i][1] + convertToRoman(num - romanValues[i][0]);
}
}
}
//Test material:
convertToRoman(36);
convertToRoman(3);
convertToRoman(-3);
convertToRoman(29);
convertToRoman(40);
convertToRoman(16);
convertToRoman(97);
convertToRoman(500);
convertToRoman(499);
convertToRoman(1023);
convertToRoman(3999);