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

rootulp's solution

to Roman Numerals in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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:


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine roman-numerals.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


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.


var toRoman = require('./roman-numerals');

describe('toRoman()', function () {
  it('converts 1', function () {

  xit('converts 2', function () {

  xit('converts 3', function () {

  xit('converts 4', function () {

  xit('converts 5', function () {

  xit('converts 6', function () {

  xit('converts 9', function () {

  xit('converts 27', function () {

  xit('converts 48', function () {

  xit('converts 59', function () {

  xit('converts 93', function () {

  xit('converts 141', function () {

  xit('converts 163', function () {

  xit('converts 402', function () {

  xit('converts 575', function () {

  xit('converts 911', function () {

  xit('converts 1024', function () {

  xit('converts 3000', function () {
function toRoman(decimal) {
  'use strict';

  var result = "";
  var numerals = [
    {decimal:    1, roman: 'I' },
    {decimal:    4, roman: 'IV'},
    {decimal:    5, roman: 'V' },
    {decimal:    9, roman: 'IX'},
    {decimal:   10, roman: 'X' },
    {decimal:   40, roman: 'XL'},
    {decimal:   50, roman: 'L' },
    {decimal:   90, roman: 'XC'},
    {decimal:  100, roman: 'C' },
    {decimal:  400, roman: 'CD'},
    {decimal:  500, roman: 'D' },
    {decimal:  900, roman: 'CM'},
    {decimal: 1000, roman: 'M' }

  for (var i = numerals.length - 1; i >= 0; i--) {
    var pairing = numerals[i];
    while (decimal >= pairing.decimal) {
      decimal -= pairing.decimal;
      result += pairing.roman;

  return result;


module.exports = toRoman

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?