Created
June 4, 2019 06:14
-
-
Save Kishanjvaghela/a7fe9cf652d8696fb945dd6235250efb to your computer and use it in GitHub Desktop.
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
function convertToRoman(input) { | |
var roman = { | |
M: 1000, | |
D: 500, | |
C: 100, | |
L: 50, | |
X: 10, | |
V: 5, | |
I: 1 | |
}; | |
var sum = 0; | |
for(var i =0; i < input.length; ){ | |
if(input[i+1] && roman[input[i]] < roman[input[i+1]]) { | |
sum = sum - roman[input[i]] + roman[input[i+1]]; | |
i = i+ 2; | |
} else { | |
sum = sum + roman[input[i]]; | |
i++; | |
} | |
} | |
console.log(sum); | |
return sum; | |
} | |
convertToRoman('CM'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment