Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Roman Numerals in the JavaScript Track

Published at Aug 26 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

 1  => I
10  => X
 7  => VII

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

See also: http://www.novaroma.org/via_romana/numbers.html


Go through the setup instructions for Javascript to install the necessary dependencies:



Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.


The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import { toRoman } from './roman-numerals';

describe('toRoman()', () => {
  test('converts 1', () => expect(toRoman(1)).toEqual('I'));
  xtest('converts 2', () => expect(toRoman(2)).toEqual('II'));
  xtest('converts 3', () => expect(toRoman(3)).toEqual('III'));
  xtest('converts 4', () => expect(toRoman(4)).toEqual('IV'));
  xtest('converts 5', () => expect(toRoman(5)).toEqual('V'));
  xtest('converts 6', () => expect(toRoman(6)).toEqual('VI'));
  xtest('converts 9', () => expect(toRoman(9)).toEqual('IX'));
  xtest('converts 27', () => expect(toRoman(27)).toEqual('XXVII'));
  xtest('converts 48', () => expect(toRoman(48)).toEqual('XLVIII'));
  xtest('converts 59', () => expect(toRoman(59)).toEqual('LIX'));
  xtest('converts 93', () => expect(toRoman(93)).toEqual('XCIII'));
  xtest('converts 141', () => expect(toRoman(141)).toEqual('CXLI'));
  xtest('converts 163', () => expect(toRoman(163)).toEqual('CLXIII'));
  xtest('converts 402', () => expect(toRoman(402)).toEqual('CDII'));
  xtest('converts 575', () => expect(toRoman(575)).toEqual('DLXXV'));
  xtest('converts 911', () => expect(toRoman(911)).toEqual('CMXI'));
  xtest('converts 1024', () => expect(toRoman(1024)).toEqual('MXXIV'));
  xtest('converts 3000', () => expect(toRoman(3000)).toEqual('MMM'));
const NUMERALS = {
  1: "I",
  4: "IV",
  5: "V",
  9: "IX",
  10: "X",
  40: "XL",
  50: "L",
  90: "XC",
  100: "C",
  400: "CD",
  500: "D",
  900: "CM",
  1000: "M"

export const toRoman = input => {
  let number = input;
  return Object.keys(NUMERALS)
    .map(key => {
      let numeral = "";

      if (key <= number) {
        let times = Math.floor(number / key);
        number -= key * times;
        for (let i = 1; i <= times; i++) {
          numeral += NUMERALS[key];
      return numeral;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?