Simple JS Solution using ES6 syntax


  • 0
    Y
    const romanToInt = (s) => {
        const romanValues = {
            I: 1,
            V: 5,
            X: 10,
            L: 50,
            C: 100,
            D: 500,
            M: 1000
        };
        const chars = s.split('');
        let sum = 0;
        
        chars.forEach((c, index, array) => {
            let current = romanValues[c];
            let next = romanValues[array[index + 1]];
            
            if (current < next) {
                sum -= current;
            } else {
                sum += current;
            }
        });
    
        return sum;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.