Skip to content

Instantly share code, notes, and snippets.

@TechWithTy
Created October 18, 2020 05:21
Show Gist options
  • Save TechWithTy/1c7787088bed185c8248f26a97933569 to your computer and use it in GitHub Desktop.
Save TechWithTy/1c7787088bed185c8248f26a97933569 to your computer and use it in GitHub Desktop.
Solution to roman numeral problem
/**
* @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