Refactoring the code using ternary conditioning operator.
var solution = (s) => { let total = 0; for(let index = 0; index < s.length; index++){ ((s.length >= index+1) && (romanMap[s[index]] < romanMap[s[index+1]])) ? (total +=romanMap[s[index+1]] - romanMap[s[index]]) && index++ : total += romanMap[s[index]]; } return total; }; const romanMap = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 };
- var solution = (s) => {
- let total = 0;
- for(let index = 0; index < s.length; index++){
if((s.length >= index + 1) && (romanMap[s[index]] < romanMap[s[index + 1]])){total += romanMap[s[index + 1]] - romanMap[s[index]];index ++;} else {total += romanMap[s[index]];}- ((s.length >= index+1) && (romanMap[s[index]] < romanMap[s[index+1]]))
- ? (total +=romanMap[s[index+1]] - romanMap[s[index]]) && index++
- : total += romanMap[s[index]];
- }
- return total;
- };
- const romanMap = {
- 'I': 1,
- 'V': 5,
- 'X': 10,
- 'L': 50,
- 'C': 100,
- 'D': 500,
- 'M': 1000
- };