Created
September 16, 2016 08:38
-
-
Save kopiro/188bcf71be02e26bb68e4a6fe76b1f7c to your computer and use it in GitHub Desktop.
Kata solution of https://www.codewars.com/kata/roman-numerals-helper
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
var RomanNumerals = { | |
fromRoman: function(roman) { | |
var map = {IV:4,IX:9,XL:40,XC:90,CD:400,CM:900,I:1,V:5,X:10,L:50,C:100,D:500,M:1000}; | |
var value = 0; | |
for (var i = 0; i < roman.length; i++) { | |
var two = map[roman[i]+roman[i+1]], one = map[roman[i]]; | |
if (two != null) { value += two; i++; } | |
else if (one != null) value += one; | |
} | |
return value; | |
}, | |
toRoman: function(value) { | |
var map = {M:1000,CM:900,D:500,CD:400,C:100,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1}; | |
var roman = ''; | |
while (value > 0) { | |
for (var r in map) { | |
if (map[r] <= value) { | |
roman += r; | |
value -= map[r]; | |
break; | |
} | |
} | |
} | |
return roman; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment