Skip to content

Instantly share code, notes, and snippets.

@mvalipour
Created December 6, 2016 01:12
Show Gist options
  • Save mvalipour/fb65d89fe1bd61bf3d71965a70b53263 to your computer and use it in GitHub Desktop.
Save mvalipour/fb65d89fe1bd61bf3d71965a70b53263 to your computer and use it in GitHub Desktop.
romanToInt
/**
* @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