Created
October 25, 2023 00:19
-
-
Save LukeberryPi/2c25e7ba76fcc21522a4b0728eaf06b6 to your computer and use it in GitHub Desktop.
roman to integer javascript function
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
function romanToInteger(roman) { | |
const romanNumerals = { | |
I: 1, | |
V: 5, | |
X: 10, | |
L: 50, | |
C: 100, | |
D: 500, | |
M: 1000, | |
}; | |
let result = 0; | |
for (let i = 0; i < roman.length; i++) { | |
const currentSymbol = romanNumerals[roman[i]]; | |
const nextSymbol = romanNumerals[roman[i + 1]]; | |
if (nextSymbol && currentSymbol < nextSymbol) { | |
result -= currentSymbol; | |
} else { | |
result += currentSymbol; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment