-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman-numerals.js
80 lines (73 loc) · 2.03 KB
/
roman-numerals.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
function fill(el, size) {
return [...Array(size)].map(() => el);
}
// Iteration 1:
// 1944 => M CM LX IV
// 4491 => IV LX CM M
export default function solution(number) {
const tokenized = number.toString().split('').reverse();
return tokenized
.map((digit, index) => {
if (index === 3) {
return fill('M', +digit).join('');
}
switch (digit) {
case '9':
if (index === 0) return 'IX';
if (index === 1) return 'XC';
if (index === 2) return 'CM';
break;
case '8':
case '7':
case '6':
if (index === 0) return `V${fill('I', digit - 5).join('')}`;
if (index === 1) return `L${fill('X', digit - 5).join('')}`;
if (index === 2) return `D${fill('C', digit - 5).join('')}`;
break;
case '5':
if (index === 0) return 'V';
if (index === 1) return 'L';
if (index === 2) return 'D';
break;
case '4':
if (index === 0) return 'IV';
if (index === 1) return 'XL';
if (index === 2) return 'CD';
break;
case '3':
case '2':
case '1':
if (index === 0) return fill('I', +digit).join('');
if (index === 1) return fill('X', +digit).join('');
if (index === 2) return fill('C', +digit).join('');
break;
}
})
.reverse()
.join('');
}
// Iteration 2
export function solution2(number) {
const mapping = [
{ char: 'M', value: 1000 },
{ char: 'CM', value: 900 },
{ char: 'D', value: 500 },
{ char: 'CD', value: 400 },
{ char: 'C', value: 100 },
{ char: 'XC', value: 90 },
{ char: 'L', value: 50 },
{ char: 'XL', value: 40 },
{ char: 'X', value: 10 },
{ char: 'IX', value: 9 },
{ char: 'V', value: 5 },
{ char: 'IV', value: 4 },
{ char: 'I', value: 1 },
];
return mapping.reduce((output, { char, value }) => {
while (number >= value) {
number -= value;
output += char;
}
return output;
}, '');
}