Created
December 24, 2018 06:40
-
-
Save kozak-iz-kh/f8a3b80b4126c68319195ae5a9f9d4ab to your computer and use it in GitHub Desktop.
Task for JavaScript developer vacancy by Ihor Tsarenko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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