logo CodeStepByStep logo

toRomanNumeral

Language/Type: JavaScript string parameters return

Write a function named toRomanNumeral that accepts an integer and returns an uppercase string representing that number in Roman numerals. For example, the call of toRomanNumeral(13) returns "XIII".

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 integer passed will be between 1 and 3999 inclusive.

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.