Created
March 25, 2023 20:54
-
-
Save devmeireles/13bfcb2d6b6d1a79a34769a553d476da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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