Skip to content

Instantly share code, notes, and snippets.

@karlwestin
Created January 27, 2012 20:55
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 karlwestin/1690875 to your computer and use it in GitHub Desktop.
Save karlwestin/1690875 to your computer and use it in GitHub Desktop.
roman/arabic conversion code kata
// while reading: http://compositecode.com/2011/09/27/code-katas-kicking-off-with-a-little-javascript/
function romanToArabic(roman) {
var letters = ["M", "D", "C", "L", "X", "V", "I"],
values = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 },
sum = 0;
for(var i=0; i<roman.length; i++) {
if (typeof roman[i+1]!= "undefined" && letters.indexOf(roman[i]) > letters.indexOf(roman[i+1]) ) {
sum += values[roman[i+1]] - values[roman[i]];
i++;
} else {
sum += values[roman[i]];
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment