Created
January 27, 2012 20:55
-
-
Save karlwestin/1690875 to your computer and use it in GitHub Desktop.
roman/arabic conversion code kata
This file contains 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
// 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