Skip to content

Instantly share code, notes, and snippets.

@Slackwise
Created October 30, 2019 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Slackwise/10d23124d0ae22f13d3a9e82393ec555 to your computer and use it in GitHub Desktop.
Save Slackwise/10d23124d0ae22f13d3a9e82393ec555 to your computer and use it in GitHub Desktop.
Convert Roman Numerals to Integers.
const values = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
const romanToInt = ([current, next, ...rest], total = 0) =>
!current
? total
: values[next] > values[current]
? romanToInt(rest, total + (values[next] - values[current]))
: romanToInt([next, ...rest], total + values[current]);
romanToInt('MMXIX'); // => 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment