logo CodeStepByStep logo

romanNumeralToInteger

Language/Type: JavaScript string parameters return

Write a function named romanNumeralToInteger that accepts a string representing a Roman numeral and returns the equivalent base-10 integer. For example, the call of romanNumeralToInteger("XIII") returns 13.

Recall that Roman numerals use the following symbols to represent the following base-10 numbers:

  • I: 1
  • V: 5
  • X: 10
  • L: 50
  • C: 100
  • D: 500
  • M: 1000

The numbers 4 and 9 are special and are written with a "minus one" prefix on the appropriate unit. In the units digit, IV and IX represent 4 and 9 respectively. In the tens digit, XL and XC represent 40 and 90 respectively. In the hundreds digit, CD and CM represent 400 and 900 respectively.

So, for example, the number 624 is represented as "DCXXIV" and the number 999 would be "CMXCIX". You may assume that the string passed will be a valid non-empty Roman numeral in uppercase using only the given characters.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.