-
Notifications
You must be signed in to change notification settings - Fork 0
/
romannum.js
105 lines (77 loc) · 2.19 KB
/
romannum.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//roman numerals 36(XXXVI)
//i, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV, XVI
// if there are three together and another is needed, a new symbol is used.
// so XXXIX(39) goes to XL (40)
//1- 100 is always the same...
//1-10 is always the same...
// 82,
// start at 100 (C)
// is bigger than 90? No, so continue to... 50(L
// is bigger than 50? No, so print L, calculate remainder (82-50=32)
// Find largest numeral less than 32 --> 10 (X)
// Print X, caluclate remainder (22)
// Find largest numeral less than 22 --> 10 X
//print x, calculate remainder (12)
// algorithm...
//find largest numeral smaller than input
//print the numeral, find the remainder,
//repeat with remainder, concatenating the strings.
// var decArr = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
// var romArr = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M']
// var s = '';
// function convertToRoman(num) {
// var numeral = '';
// var numeralindex = 0;
// var number = 0;
// for (i = 0; i < decArr.length; i++) {
// if (num < decArr[i]) {
// number = decArr[i-1];
// numeralindex = i-1;
// break;
// };
// if (num == decArr[i]) {
// number = decArr[i];
// numeralindex = i;
// break;
// }
// }
// s = s.concat(romArr[numeralindex]);
// var remainder = num - number;
// console.log(s)
// if (remainder == 0) {
// return s;
// }
// else {
// convertToRoman(remainder);
// }
// }
// var example = convertToRoman(86);
// console.log(example);
var decArr = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000, 5000]
var romArr = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M', 'V']
var s = '';
function convertToRoman(num) {
var numeralindex = 0;
var newnumber = 0;
var s = '';
var remainder = num;
while (remainder > 0 ) {
for (i = 0; i < decArr.length; i++) {
if (remainder < decArr[i]) {
newnumber = decArr[i-1];
numeralindex = i-1;
break;
};
if (remainder == decArr[i]) {
newnumber = decArr[i];
numeralindex = i;
break;
}
}
s = s.concat(romArr[numeralindex]);
remainder = remainder - newnumber;
}
return s;
}
var example = convertToRoman(1086);
console.log(example);