From Roman to Modern Times

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

Natalie Galligan
Aug 16, 2020 · 3 min read
Photo by Thomas Bormans on Unsplash

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:

Resources:

Explore the world of web technologies through a series of tutorials

By Weekly Webtips

Get the latest news on the world of web technologies with a series of tutorial Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Natalie Galligan

Written by

Developer conference organizer => software engineer! Follow me for stories what’s it like to be in a coding bootcamp and start your career in tech ;)

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store