-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.js
41 lines (33 loc) · 947 Bytes
/
roman.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
console.log('Welcome to the fun!');
const map = {
"I": { val: 1, limit: 3 },
"V": { val: 5, limit: 1 },
"X": { val: 10, limit: 3 },
"L": { val: 50, limit: 1 },
"C": { val: 100, limit: 3 },
"D": { val: 500, limit: 1 },
"M": { val: 1000, limit: 3 },
};
module.exports = (roman) => {
let repeatCharacter;
let repeatCount = 0;
return [...roman].reduce((prev, curr, index) => {
const current = map[curr];
if (curr === repeatCharacter) {
repeatCount++;
if (repeatCount >= current.limit) {
throw "InvalidArgument";
}
} else {
repeatCount = 0;
}
repeatCharacter = curr;
const currNum = current.val;
let sum = prev + currNum;
const last = map[roman[index - 1]];
if (last && last.val < currNum) {
sum -= last.val * 2;
}
return sum;
}, 0);
};