← Back to challenges

Roman Numerals

JavaScriptHardnumbersstringsmath

Instructions

Create a function that takes in a Roman numeral as a string and converts it to an integer, returning the result. The function should work for all Roman numerals representing positive integers less than 4000.

The following table shows how digits will be represented in Roman numeral notation:

Place value123456789
OnesIIIIIIIVVVIVIIVIIIIX
TensXXXXXXXLLLXLXXLXXXXC
HundredsCCCCCCCDDDCDCCDCCCCM
ThousandsMMMMMM------

Examples

parseRomanNumeral("VII") ➞ 7

parseRomanNumeral("DCLXXIX") ➞ 679

parseRomanNumeral("MMMCMV") ➞ 3905

Notes

  • All letters will be in uppercase.
  • Assume all inputs will be well-formed Roman numerals.
  • While you could probably solve this by separately checking for each of these sequences inside the string, there is a smarter way. Think about the numerical value each individual letter has, and how the letter immmediately following it can affect that letter's numerical value.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.