Skip to content

Instantly share code, notes, and snippets.

@kozak-iz-kh
Created December 24, 2018 06:40
Show Gist options
  • Save kozak-iz-kh/f8a3b80b4126c68319195ae5a9f9d4ab to your computer and use it in GitHub Desktop.
Save kozak-iz-kh/f8a3b80b4126c68319195ae5a9f9d4ab to your computer and use it in GitHub Desktop.
Task for JavaScript developer vacancy by Ihor Tsarenko
https://leetcode.com/problems/roman-to-integer/
Here is my solution:
const romanToInt = str => {
const VALUES = {
M: 1000,
D: 500,
C: 100,
L: 50,
X: 10,
V: 5,
I: 1
};
let prev = 0;
let sum = 0;
for (let i = 0; i < str.length; i++) {
let curr = VALUES[str[i]];
sum += prev < curr ? curr - 2 * prev : curr;
prev = curr;
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment