Today, we’re going to explore how to convert Roman numerals into integers using a simple yet effective JavaScript function. Whether you’re a coding enthusiast, a student, or just curious about programming, this tutorial is for you!

Let’s begin by looking at the function we’ll be using. It’s called romanToInt and it takes a string s as an input, which represents the Roman numeral we want to convert.

So, we have a variable with a Roman numeral

const s = 'MCMXCIV';

1. Function Definition:

First of all let’s defines a function named romanToInt that takes a single argument s, which is expected to be a string representing a Roman numeral.

let romanToInt = (s) => { ... };

2. Create a Roman Numeral Symbols Object:

This is a JavaScript object that maps Roman numeral symbols (I, V, X, L, C, D, M) to their corresponding integer values.

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

3. Initialization of Result Variable:

Next, we initialize a variable named result to zero. This will hold our final integer value.

 
 let result = 0;

This initializes a variable result to 0. This variable will accumulate the final integer value of the Roman numeral.

4. Loop Through the String:

We then use a for-loop to iterate through each character of the input string.

for (let i = 0; i < s.length; i++) { ... }

5. Extract Current and Next Symbol Values:

Inside this loop, we have two key variables: current and next. current holds the integer value of the current Roman numeral character, and next holds the value of the next character.

const current = symbols[s[i]];
const next = symbols[s[i + 1]];

For each character in s, it gets the corresponding value from the symbols object.

current = symbol[s[i]] => symbol["M"] => 1000 
next = symbol[s[i]] => symbol["C"] =>100

6. Conditional Logic for Value Calculation:

Inside the loop:

The magic happens in this conditional statement. If the current value is less than the next value, it means we need to subtract current from next and add the result to our total. This accounts for cases like IV, where I precedes a larger value, V, indicating 4.”


if (current < next) {
      result += next - current;
      i++;
    } else {
      result += current;
    }

This is a conditional statement to handle the Roman numeral subtraction rule. In Roman numerals, if a smaller number precedes a larger number, it means subtraction. If “current” is less than “next”, it subtracts “current” from “next”, adds this to result, and increments “i” to skip the next character (since it’s already processed). Otherwise (if “current” is greater than or equal to “next”), it simply adds “current” to result. 7.Return the Result:

return result;

After the loop completes, the function returns result, which is the integer representation of the Roman numeral.

Let’s see the complete code:


const s = 'MCMXCIV';

let romanToInt = (s) => {
  const symbols = {
    I: 1,
    V: 5,
    X: 10,
    L: 50,
    C: 100,
    D: 500,
    M: 1000,
  };

  let result = 0;

  for (let i = 0; i < s.length; i++) {
    
    const current = symbols[s[i]];
    const next = symbols[s[i + 1]];

    if (current < next) {
      result += next - current;
      i++;
    } else {
      result += current;
    }
  }
  return result;
};

The result will be 1994.

Thank you for reading, and happy coding!