Created
October 18, 2020 05:21
-
-
Save TechWithTy/1c7787088bed185c8248f26a97933569 to your computer and use it in GitHub Desktop.
Solution to roman numeral problem
This file contains hidden or 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) { | |
romanNumerals = { | |
I: 1, | |
V: 5, | |
X: 10, | |
L: 50, | |
C: 100, | |
D: 500, | |
M: 1000, | |
} | |
romanArr = s.split(''); | |
let result = 0; | |
romanArr.forEach((numeral,i) =>{ | |
romanNum = romanNumerals[numeral]; | |
romanNumNext = romanNumerals[s[i+1]] | |
if(romanNum < romanNumNext){ | |
result -= romanNumerals[numeral] | |
}else{ | |
result += romanNumerals[numeral]; | |
} | |
}) | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment