Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

Published at Jul 13 2018
·
0 comments

Instructions

Test suite

Solution

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:

http://exercism.io/languages/javascript/installation

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

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 () {
expect(toRoman(1)).toEqual('I');
});
xit('converts 2', function () {
expect(toRoman(2)).toEqual('II');
});
xit('converts 3', function () {
expect(toRoman(3)).toEqual('III');
});
xit('converts 4', function () {
expect(toRoman(4)).toEqual('IV');
});
xit('converts 5', function () {
expect(toRoman(5)).toEqual('V');
});
xit('converts 6', function () {
expect(toRoman(6)).toEqual('VI');
});
xit('converts 9', function () {
expect(toRoman(9)).toEqual('IX');
});
xit('converts 27', function () {
expect(toRoman(27)).toEqual('XXVII');
});
xit('converts 48', function () {
expect(toRoman(48)).toEqual('XLVIII');
});
xit('converts 59', function () {
expect(toRoman(59)).toEqual('LIX');
});
xit('converts 93', function () {
expect(toRoman(93)).toEqual('XCIII');
});
xit('converts 141', function () {
expect(toRoman(141)).toEqual('CXLI');
});
xit('converts 163', function () {
expect(toRoman(163)).toEqual('CLXIII');
});
xit('converts 402', function () {
expect(toRoman(402)).toEqual('CDII');
});
xit('converts 575', function () {
expect(toRoman(575)).toEqual('DLXXV');
});
xit('converts 911', function () {
expect(toRoman(911)).toEqual('CMXI');
});
xit('converts 1024', function () {
expect(toRoman(1024)).toEqual('MXXIV');
});
xit('converts 3000', function () {
expect(toRoman(3000)).toEqual('MMM');
});
});
```

```
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
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments