Skip to content

Instantly share code, notes, and snippets.

@fernandoporazzi
Created March 7, 2014 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandoporazzi/9419350 to your computer and use it in GitHub Desktop.
Save fernandoporazzi/9419350 to your computer and use it in GitHub Desktop.
Convert Roman Numerals to Arabic Numbers - Javascript
/*!
Source code to convert Roman numerals to Arabic numbers.
I hope it help someone!
2014-03-07
*/
var i = 0;
var chars = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
};
function convert(rom) {
var arabic = 0;
for (i = 0; i < rom.length; i++) {
var currentChar = rom[i],
nextChar = rom[i + 1];
if (chars[currentChar] < chars[nextChar]) {
arabic -= chars[currentChar];
} else {
arabic += chars[currentChar];
}
}
console.log(arabic);
}
convert('MDXXIV');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment