forked from kloklo90/LeetcodeChallenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanToInt.js
63 lines (62 loc) · 1.3 KB
/
RomanToInt.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
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
// Substract Cases are taken care of by substituing them with our own new literals
// A = 4 // B = 9 // E = 40 // F = 90 // G = 400 // H = 900
var myOwnLiteral = s.replace("IV", "A")
.replace("IX", "B")
.replace("XL", "E")
.replace("XC", "F")
.replace("CD", "G")
.replace("CM", "H");
var valueLiteral = myOwnLiteral.split("");
var value = 0;
valueLiteral.forEach(function(literal) {
switch(literal) {
case 'M':
value += 1000;
break;
case 'D':
value += 500;
break;
case 'C':
value += 100;
break;
case 'L':
value += 50;
break;
case 'X':
value += 10;
break;
case 'V':
value += 5;
break;
case 'I':
value += 1;
break;
case 'A':
value += 4;
break;
case 'B':
value += 9;
break;
case 'E':
value += 40;
break;
case 'F':
value += 90;
break;
case 'G':
value += 400;
break;
case 'H':
value += 900;
break;
default:
break;
}
});
return value;
};