Last active
June 21, 2018 15:33
-
-
Save elmahdim/69fbac65edfc1f3fa0258cf765b30e56 to your computer and use it in GitHub Desktop.
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
/** | |
* @param {string} s | |
* @return {number} | |
*/ | |
const romanToInt = s => { | |
const chart = { | |
I: 1, | |
V: 5, | |
X: 10, | |
L: 50, | |
C: 100, | |
D: 500, | |
M: 1000 | |
}; | |
let output = 0; | |
[...s].forEach((value, index) => { | |
const current = chart[s[index]]; | |
output += current < chart[s[index + 1]] || 0 ? -current : current; | |
}); | |
return output; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment