Skip to content

Commit 1ca4dad

Browse files
13- - roman to int problem
1 parent d6c48ae commit 1ca4dad

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cSpell.words": ["Palindrom"]
2+
"cSpell.words": ["MCMXCIV", "Palindrom"]
33
}

13-romatToInt.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const romanToInt = (s) => {
2+
const roman = {
3+
I: 1,
4+
V: 5,
5+
X: 10,
6+
L: 50,
7+
C: 100,
8+
D: 500,
9+
M: 1000,
10+
};
11+
let sum = 0;
12+
for (let i = 0; i < s.length; i++) {
13+
if (roman[s[i]] < roman[s[i + 1]]) {
14+
sum += roman[s[i + 1]] - roman[s[i]];
15+
i++;
16+
} else {
17+
sum += roman[s[i]];
18+
}
19+
}
20+
return sum;
21+
};
22+
23+
console.log(romanToInt('MCMXCIV'));

0 commit comments

Comments
 (0)