Skip to content

Commit 03535e8

Browse files
committed
13
1 parent 2290339 commit 03535e8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

13-roman-to-integer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var romanToInt = function(s) {
6+
const romanMap = {
7+
'I': 1,
8+
'V': 5,
9+
'X': 10,
10+
'L': 50,
11+
'C': 100,
12+
'D': 500,
13+
'M': 1000
14+
}
15+
16+
let total = 0;
17+
18+
for (let i = 0; i < s.length; i++) {
19+
const currValue = romanMap[s[i]];
20+
const nextValue = romanMap[s[i + 1]];
21+
22+
if (currValue < nextValue) {
23+
total -= currValue;
24+
} else {
25+
total += currValue;
26+
}
27+
}
28+
29+
return total;
30+
};

0 commit comments

Comments
 (0)