const s = "IX";
function romanToNum(s) {
let newArr = s.split("");
let answer = 0;
let divider = 0;
let formatted = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
answer += formatted[newArr[0]];
for (let i = 1; i < s.length; i++) {
if (formatted[newArr[i]] % 5 === 0) {
if (formatted[newArr[i - 1]] < formatted[newArr[i]]) {
divider = formatted[newArr[i]] / 5;
if (divider === Math.pow(10, formatted[newArr[i]].toString().length-1)) {
answer += formatted[newArr[i]] - divider * 2;
} else {
answer += formatted[newArr[i]] - divider;
}
} else {
answer += formatted[newArr[i]];
}
} else {
answer += formatted[newArr[i]]
}
}
return answer;
}
romanToNum(s);