# From Roman to Modern Times

## Or how to make your machine read Roman numerals with this JavaScript algorithm

We’re so used to our regular numbers — 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 — that we forget we haven’t always had those. There were quite a few numeral systems in the history of mankind, but most of them are no longer in use. However, Roman numerals are still in (sort of) use, especially if you have a grandfather’s clock and want to know the time. 😁

Most of us have been taught how to read Roman numerals as kids and we do that almost instinctively. Here’s a primer how to read them:

Roman numerals are represented by seven different symbols: `I`

, `V`

, `X`

, `L`

, `C`

, `D`

and `M`

.

**Symbol** **Value**

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

For example, 2 is written as `II`

in Roman numeral — just two one's added together. 12 is written as `XII`

, which is `X`

+ `II`

. 27 is written as `XXVII`

, which is `XX`

+ `V`

+ `II`

.

Roman numerals are usually written largest to smallest from left to right. However, as numbers increase, at some point, it makes more sense to subtract the lower number from the larger number. For example, 9 is not `VIIII`

but `IX,`

40 is not `XXXX`

but `XV`

, etc. It’s easy to explain that to a fellow human, but how can we explain that to a machine? We need to write a fool-proof algorithm to do that!

First, let’s just translate Roman numerals to regular numbers in the order they appear. For example, XLVII would be 10, 50, 5, 1, 1. To do that we’ll use a for loop, and we’ll be checking if the current numeral in the string (s) is one of the Roman numerals. For example, if it’s `I`

, we’ll be adding 1 to the array of numbers that we created beforehand, and if it’s `X`

, we’ll be adding 10.

Here’s how this loop will look like:

In our example, our number is `XLVII`

. We know in our hearts that the number is 47, but how can we logically explain to the machine that `[10, 50, 5, 1, 1]`

is, in fact, 47?

The core idea is that in Roman numerals, the number on the left should be larger than the one on the right.

So what we can do is look at the current number and the next number, and if the current number is less than the next number, it’ll be subtracted from the next number. In our example, 10 is less than 50, so we’ll subtract 10 from 50 and we’ll get 40.

So let’s iterate through the array and check the value of numbers that are next to each other. Let’s use a while loop for that.

Let’s remove the first number of the array with splice() and then compare its value to the number that’s now first in the array. We’ll continue doing that until we don’t have anything left in the array of numbers.

If the removed number is larger than the next one, or if it’s the very last number, we just add it to the total. I do firstNumber[0] because splice returns an array, and I need the first and only element there.

If the removed number is less than the next number, we subtract it from the total.

And in the end, we’ll just return total!

Here’s how the full code looks like: