Here are a couple of utility functions to deal with Roman numerals in both ways.

const romans = {
  'M': 1000,
  'CM': 900,
  'D': 500,
  'CD': 400,
  'C': 100,
  'XC': 90,
  'L': 50,
  'XL': 40,
  'X': 10,
  'IX': 9,
  'V': 5,
  'IV': 4,
  'I': 1
};

function toRomans(n) {
  let result = '';

  Object.entries(romans).forEach(([roman, value]) => {
    while (value <= n) {
      result += roman;
      n -= value;
    }
  });

  return result;
}

function fromRomans(s) {
  const keys = Object.keys(romans);
  const splitted = s.split('');

  return splitted.reduce((acc, cur, index) => {
    if (index + 1 < splitted.length && keys.indexOf(cur) > keys.indexOf(splitted[index + 1])) {
      return acc + (romans[splitted[index + 1]] - romans[cur]) - romans[splitted[index + 1]];
    }

    return acc + romans[cur];
  }, 0);
}

console.log(toRomans(1998)); // "MCMXCVIII"
console.log(fromRomans("MCMXCVIII")); // 1998