-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertToRomanNos.js
More file actions
46 lines (35 loc) · 1.07 KB
/
Copy pathconvertToRomanNos.js
File metadata and controls
46 lines (35 loc) · 1.07 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
function convertToRoman(num) {
var romanNos = ["I", "V", "X", "L", "C", "D", "M"];
var decNos = [1, 5, 10, 50, 100, 500, 1000];
var numStr = num.toString();
var numArr = numStr.split('');
numArr.forEach(function (ele, idx, arr) {
console.log(idx);
var tkp = arr.length - idx - 1;
arr[idx] = {
num: parseInt(ele) * Math.pow(10, tkp),
digit: ele
}
});
var decNum = "";
numArr.forEach(function (ele, idx, arr) {
var rIdx = decNos.indexOf(ele);
if (rIdx != -1) {
decNum += romanNos[rIdx];
} else {
}
});
console.log(numArr);
}
function convertToRoman2(num) {
var romanNos = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
var decNos = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
var romanStr = "";
for (var i = decNos.length - 1; i >= 0; i--) {
while (decNos[i] <= num) {
romanStr += romanNos[i];
num -= decNos[i];
}
}
console.log(romanStr);
}