Roman Numeral Translator

Welcome coders of the old and new, today we shall talk about numbers specifically how the Romans did it. First off what the hell even is a Roman Numeral, let's see what google says. Roman Numerals are a numeral system that originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the Late Middle Ages. Numbers in this system are represented by combinations of letters from the Latin alphabet. Welp, Google knows all so yeah I guess its a way of telling time. So I'm not a big fan of the roman system and can't quite get it so, I decided to write a translator for it. But before I did that I went and scoured the internet for a code challenge that was close to what I was trying to accomplish. You know just to make it a bit harder and clearer of what I needed to do exactly, something about being autonomous.
Came across one that seemed promising.

Given a roman numeral as input,
write a function that converts the roman numeral to a number and outputs it.
You've been provided a helper DIGIT_VALUES to use.
Input: Strings of Roman Numerals
Output: Number representation of Roman Numeral
Cases:
If you receive an empty string, return 0.
If you receive anything other than a string, return null.

Examples =>
VI = 6 (5 + 1 = 6)
LXX = 70 (50 + 10 + 10 = 70)
MCC = 1200 (1000 + 100 + 100 = 1200)
IV = 4 (5 β 1 = 4)
XC = 90 (100 β 10 = 90)
CM = 900 (1000 β 100 = 900)


Here's The Helper

const DIGIT_VALUES = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};


Let's look at an example of how my roman numerals translator looks in code, since I need to make everything code related to understanding it.

const translateRomanNumeral = (romanNumeral) => {
if (typeof (romanNumeral) !== 'string') {
return null;
}
if (romanNumeral === '') {
return 0;
}
let number = 0;
for (let i = romanNumeral.length - 1; i > -1; i--) {
if (DIGIT_VALUES[romanNumeral[i]] < DIGIT_VALUES[romanNumeral[i + 1]]) {
number -= DIGIT_VALUES[romanNumeral[i]];
} else {
number += DIGIT_VALUES[romanNumeral[i]];
}
}
return number;
};

// test
translateRomanNumeral('IV'); // => 4
translateRomanNumeral('V'); // => 5
translateRomanNumeral('X'); // => 10
translateRomanNumeral('XV'); // => 15


So let's do a bit of explaining, we know that we need to check a letter in the given string against the ones in our DIGIT_VALUES object. Also depending on the next number, we need to also follow this cadence of adding and subtracting. We can handle the cases we are given by doing a couple of conditionals. Checking if the string is even an actual string we want to return null. If the string is empty like '', then we want to send back 0. Other then that we do some looping and test if a given letter in the passed string is going to be added or subtracted, then doing the following. When a smaller numeral appears before a larger one, it becomes a subtractive operation. You can assume only one smaller numeral may appear in front of the larger one. With that, we can use a quick conditional chain that will determine which way we operate on our numerals.
Finally, no function is complete without our return statement here we just return the number variable we created earlier. I found doing this code challenge interesting because using a system like this for numbers seems like it could get really confusing. Even though many people used it for a long time so must have been good enough for them.

Posted on by:

NaseerHines

Just a gamer learning some code.