Skip to content

Instantly share code, notes, and snippets.

@devmeireles
Created March 25, 2023 20:54
Show Gist options
  • Save devmeireles/13bfcb2d6b6d1a79a34769a553d476da to your computer and use it in GitHub Desktop.
Save devmeireles/13bfcb2d6b6d1a79a34769a553d476da to your computer and use it in GitHub Desktop.
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function (s) {
const base = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
const sArr = s.split('');
let value = 0;
sArr.forEach((item, index) => {
let curr = base[sArr[index]]
let next = base[sArr[index + 1]]
curr < next ? value -= curr : value += curr
});
return value
};
console.log(romanToInt('III'))
console.log(romanToInt('XXV'))
console.log(romanToInt('IX'))
console.log(romanToInt('MMDC'))
console.log(romanToInt('DC'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment