Create Roman to number mapping lists, ['M', 'D', 'C', 'L', 'X', 'V', 'I'] and [1000, 500, 100, 50, 10, 5, 1]. Iterate over each char in Ramon number. From Roman list, if next char's position is less than current char's, add number[next char's position from Roman list] - number[current char's position from Roman list] to ouput and increase i's position by 2. Otherwise, add number[current char's position from Roman list] to output and increase i's position by 1.
data = "MCMIV"
rs = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
ns = [1000, 500, 100, 50, 10, 5, 1]
output = 0
i = 0
while i < len(data):
  p1 = rs.index(data[i])
  p2 = rs.index(data[i + 1]) if (i + 1 < len(data)) else float('inf')
  if p2 < p1:
    output += ns[p2] - ns[p1]
    i += 1
  else:
    output += ns[p1]
  i += 1
print(output)
const data = "MCMIV";
const rs = ['M', 'D', 'C', 'L', 'X', 'V', 'I'];
const ns = [1000, 500, 100, 50, 10, 5, 1];
let output = 0;
for (let i = 0; i < data.length; i++) {
  const p1 = rs.indexOf(data.at(i));
  const p2 = (i + 1 < data.length) ? rs.indexOf(data.at(i + 1)) : Infinity;
  
  if (p2 < p1) {
    output += ns[p2] - ns[p1];
    i++;
  } else {
    output += ns[p1];
  }
}
console.log(output);