Public Solving: Converting Roman numerals to Arabic

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today the elves asked us to help with a Roman numeral converter in JavaScript.
You can find the complete puzzle here.
You might have seen Roman numerals before. They look like this:
I = 1
IV = 4
V = 5
VIII = 8
XCV = 95
The above is what we must do, convert the Roman numerals to the Arabic numeric version.
Luckily for us, there are some rules to Roman numerals that can help us!
I had quite a hard time thinking about the smallest possible codebase.
At first thought about adding IV as a value option and filtering out negative numbers.
Thinking a bit more and reading the Roman rules on using the letters, we can filter this out quickly!
All we need to do is check which number was preceding. If this number is smaller, it's a negative one!
And that sparked me to write a super simple reduce method that does all the heavy lifting for us.
Let's see how it works.
The first thing I did was add a mapping object. This object contains all the Roman letters and their representing value.
const chart = {
M: 1000,
D: 500,
C: 100,
L: 50,
X: 10,
V: 5,
I: 1,
};
The next thing we need to do is convert the input into an array to use JavaScript array methods.
At this time, I also decided to uppercase it since that's what our mapping tables accept.
input.toUpperCase().split('')
Then we want to use the JavaScript reduce() method. This method is excellent for this purpose because it can pass an accumulator (previous value).
return input
.toUpperCase()
.split('')
.reduce(
(acc, romanLetter) => {
// Todo
},
[0, 0]
);
Let me describe what's going on here.
We reduce the array we just created, and then we get the following parameters:
acc: The accumulator contains the previous value and starts with the default.romanLetter: The current looped element[0, 0]: This is the default value. I'm using an array to keep track of the total sum and the previous single value.Then we need to retrieve the value of this roman letter.
const arabicValue = chart[romanLetter];
We can then simply return the total value of our number and the current single value like this.
return [acc[0] += arabicValue, arabicValue];
This works great, as long as there is no negative value like IV.
To fix that, we can introduce a negative offset. We will check if the previous single value is smaller than the current one. We should subtract 2 * from the previous value if that is true.
We do 2 times the value since we just added it in the previous loop, and it's actually a subtraction of that specific value.
let negativeOffset = 0;
if (acc[1] < arabicValue) {
negativeOffset = -(acc[1] * 2);
}
And then, we can simply + this negative value to our total value.
return [(acc[0] += arabicValue + negativeOffset), arabicValue];
Now, in the end, we just need to return only the total value, which is array element 0 from our reduce.
export const romanToArabic = (input) => {
return input
.toUpperCase()
.split('')
.reduce(
(acc, romanLetter) => {
let negativeOffset = 0;
const arabicValue = chart[romanLetter];
if (acc[1] < arabicValue) {
negativeOffset = -(acc[1] * 2);
}
return [(acc[0] += arabicValue + negativeOffset), arabicValue];
},
[0, 0]
)[0];
};
Now let's try and run the test to see how we did:

This was quite a cool one to do, and I'm sure there are 100 and 1 good solutions. Let me know what you think of this one or do differently.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter