Skip to content

Instantly share code, notes, and snippets.

@elmahdim
Last active June 21, 2018 15:33
Show Gist options
  • Save elmahdim/69fbac65edfc1f3fa0258cf765b30e56 to your computer and use it in GitHub Desktop.
Save elmahdim/69fbac65edfc1f3fa0258cf765b30e56 to your computer and use it in GitHub Desktop.
/**
* @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