-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanNumeralConverter.js
More file actions
127 lines (91 loc) · 2.99 KB
/
RomanNumeralConverter.js
File metadata and controls
127 lines (91 loc) · 2.99 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function convertToRoman(num) {
var romanNumeralArray = ['I', 'V', 'X', 'L', 'C', 'D', 'M', 'v', 'x'];
var romanNumeralSet = [];
var romanNumeral = "";
var romanNumeralObject;
var baseTenDigit;
//write for loop with terminating condition being length
if (num > 0) {
baseTenDigit = num.toString().slice(-1);
romanNumeralSet = romanNumeralArray.slice(0, 3);
romanNumeral = printRomanNumeral(baseTenDigit, romanNumeralSet);
}
if (num >= 10) {
baseTenDigit = num.toString().slice(-2, -1);
romanNumeralSet = romanNumeralArray.slice(2, 5);
romanNumeral = printRomanNumeral(baseTenDigit, romanNumeralSet) + romanNumeral;
}
if (num >= 100) {
baseTenDigit = num.toString().slice(-3, -2);
romanNumeralSet = romanNumeralArray.slice(4, 7);
romanNumeral = printRomanNumeral(baseTenDigit, romanNumeralSet) + romanNumeral;
}
if (num >= 1000) {
baseTenDigit = num.toString().slice(-4, -3);
romanNumeralSet = romanNumeralArray.slice(6, 9);
romanNumeral = printRomanNumeral(baseTenDigit, romanNumeralSet) + romanNumeral;
}
return romanNumeral;
}
console.log(convertToRoman(1000));
//given a roman numeral set and roman numaral object print a roman numeral that represents a base 10 digit; return a RN representation of base10 digit, eg "IX"
function printRomanNumeral(digit, romanNumeralSet) {
var ones = romanNumeralSet[0];
var fives = romanNumeralSet[1];
var tens = romanNumeralSet[2];
var romanNumeral = '';
digit = parseInt(digit);
//if 1-3 print 1-3 "1"s
if (digit < 4) {
for (var i = digit; i > 0; i--) {
romanNumeral += ones;
}
}
//if 4 print 1 "1" && print one "5"
if (digit === 4) {
romanNumeral = ones + fives;
}
// if 5 print NO "1" && print "5"
if (digit === 5) {
romanNumeral = fives;
}
// if 6-8 print 6-8 - 5 "1"s
if (digit > 5 && digit < 9) {
romanNumeral = fives;
for (var i = digit - 5; i > 0; i--) {
romanNumeral += ones;
}
}
// if 9 print 1 "1" and a "10"
if (digit === 9) {
romanNumeral = ones + tens;
}
return romanNumeral; //STRING VALUE TO BUILD UP ROMAN NUMERAL
}
// function convertDigit(digit) {
// digit = parseInt(digit);
// var romanNumeralObject = {
// ones: 0,
// fives: 0,
// tens: 0
// }
// if (digit < 4) {
// romanNumeralObject.ones = digit;
// }
// if (digit === 4) {
// romanNumeralObject.ones = 1;
// romanNumeralObject.fives = 1;
// }
// if (digit === 5) {
// romanNumeralObject.fives = 1;
// }
// if (digit > 5 && digit < 9) {
// romanNumeralObject.fives = 1;
// romanNumeralObject.ones = digit - 5;
// }
// if (digit === 9) {
// romanNumeralObject.ones = 1;
// romanNumeralObject.tens = 1;
// }
// return romanNumeralObject;
// }