-
Notifications
You must be signed in to change notification settings - Fork 4
/
romanNumeralsDecoder.js
41 lines (38 loc) · 1.36 KB
/
romanNumeralsDecoder.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
/**
* Description:
*
* Create a function that takes a Roman numeral as its argument and returns
* its value as a numeric decimal integer. You don't need to validate the form
* of the Roman numeral.
*
* Modern Roman numerals are written by expressing each decimal digit of
* the number to be encoded separately, starting with the leftmost digit
* and skipping any 0s. So 1990 is rendered "MCMXC" (1000 = M, 900 = CM, 90 = XC)
* and 2008 is rendered "MMVIII" (2000 = MM, 8 = VIII).
* The Roman numeral for 1666, "MDCLXVI", uses each letter in descending order.
*
* Example:
*
* solution('XXI'); // should return 21
*/
function solution(roman){
let decode = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M':1000}
return roman.split('').reduce((sum, curr, i) => {
let nextChar = roman.charAt(i+1)
switch(curr) {
case 'I':
sum += nextChar.includes('X') || nextChar.includes('V') ? -1 : decode[curr]
break
case 'X':
sum += nextChar.includes('L') || nextChar.includes('C') ? -10 : decode[curr]
break
case 'C':
sum += nextChar.includes('D') || nextChar.includes('M') ? -100 : decode[curr]
break
default:
sum += decode[curr]
break
}
return sum
}, 0);
}