Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.