-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertRoman.js
76 lines (57 loc) · 2.13 KB
/
convertRoman.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
// function that given a decimal number converts it to its roman number
function convertToRoman(num) {
// How Roman Numerals work : https://www.mathsisfun.com/roman-numerals.html
let roman = {
1: "I",
5: "V",
10: "X",
50: "L",
100: "C",
500: "D",
1000: "M"
}
/*
* 1) Get the number of units and convert the number accordingly to its roman number
* 2) Repeat step 1) for the remaing digits (tens digits, hundreds, etc)
*/
let convertedNum = "";
let numDigits = num.toString().length;
for (let i=numDigits-1; i>=0; i--) {
// position of the digit (units, tens, hundreds, etc)
let pos = Math.pow(10, i);
// get digit of the current position
// and convert to its whole number
/* for example, if we have the number 192
* and we wish to obtain the tens digit
* we do ((192/10) % 10) = 9
*/
let digit = Math.floor(((num/pos) % 10));
if (digit == 9 || digit == 4) {
// Taking the previous example,
// 90 = 100 - 10 = roman[10] + roman[9 * 10 + 10]
convertedNum += roman[pos] + roman[digit * pos + pos];
} else {
// get the biggest number (property in 'roman') that comes right before 'number'
function largestRoman(roman, number, pos) {
let max = 0;
// array of numbers declared on the object 'roman'
let numbers = Object.getOwnPropertyNames(roman);
for (let j=0; j<numbers.length; j++) {
let n = Number.parseInt(numbers[j]);
if (n > number) break;
if (max < n && n <= number)
max = n;
}
return max;
}
// while there are digits to be converted
let rest = digit * pos;
while (rest > 0) {
let max = largestRoman(roman, rest, pos);
convertedNum += roman[max];
rest -= max;
}
}
}
return convertedNum;
}