var romanToInt = function (s) { let result = 0; for (let i = 0; i < s.length; i++) { switch (s[i]) { case'I': result += 1 break; case'V': result += 5 break; case'X': result += 10 break; case'L': result += 50 break; case'C': result += 100 break; case'D': result += 500 break; case'M': result += 1000 break; } }
letgetValue = (s) => { let result; switch (s) { case'I': result = 1 break; case'V': result = 5 break; case'X': result = 10 break; case'L': result = 50 break; case'C': result = 100 break; case'D': result = 500 break; case'M': result = 1000 break; }
return result }
var romanToInt = function (s) { if (s.length === 1) { returngetValue(s) }
let result = 0; for (let i = 0; i < s.length; i++) { const currentValue = getValue(s[i]) const nextValue = getValue(s[i + 1 > s.length ? i : i + 1]) if (currentValue < nextValue) { result -= currentValue } else { result += currentValue } }