Created
December 6, 2016 01:12
-
-
Save mvalipour/fb65d89fe1bd61bf3d71965a70b53263 to your computer and use it in GitHub Desktop.
romanToInt
This file contains hidden or 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} | |
*/ | |
var romanToInt = function(s) { | |
var map = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000, | |
}; | |
var max = 0; | |
return s.split('').reverse().reduce((r, e) => { | |
var v = map[e]; | |
r += (v < max ? -v : v); | |
if(v > max) max = v; | |
return r; | |
}, 0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment