Skip to content

Instantly share code, notes, and snippets.

@LukeberryPi
Created October 25, 2023 00:19
Show Gist options
  • Save LukeberryPi/2c25e7ba76fcc21522a4b0728eaf06b6 to your computer and use it in GitHub Desktop.
Save LukeberryPi/2c25e7ba76fcc21522a4b0728eaf06b6 to your computer and use it in GitHub Desktop.
roman to integer javascript function
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