Roman Numeral Code Kata
Implement a method to convert arabic numerals to roman numerals
HTML
<p><strong>RomanNumeralConverter.convert(x)</strong> should:</p>
<ul id="tests">
</ul>
<input id="in" type="number" />
<button id="do">Convert</button>
<span id="out"></span>
CSS
body { font-family: Arial, Sans-serif; font-size: 14px; color:#444; }
ul { list-style-type: none; padding: 1em; margin: 0; }
li { margin-bottom: 1em; }
.fail span { color: red; }
.pass span { color: green; }
JavaScript
var RomanNumeral = (function () {
function RomanNumeral(numeral, arabicValue) {
this.numeral = numeral;
this.arabicValue = arabicValue;
if (arguments.length === 3) {
this.canBeModifiedBy = arguments[2];
}
}
RomanNumeral.prototype.matches = function (arabicValue) {
if (this.arabicValue <= arabicValue) {
return this;
}
if (this.canBeModifiedBy && this.arabicValue - this.canBeModifiedBy.arabicValue <= arabicValue) {
return new RomanNumeral(this.canBeModifiedBy.numeral + this.numeral, this.arabicValue - this.canBeModifiedBy.arabicValue);
}
return null;
};
RomanNumeral.I = new RomanNumeral("I", 1);
RomanNumeral.V = new RomanNumeral("V", 5, RomanNumeral.I);
RomanNumeral.X = new RomanNumeral("X", 10, RomanNumeral.I);
RomanNumeral.L = new RomanNumeral("L", 50, RomanNumeral.X);
RomanNumeral.C = new RomanNumeral("C", 100, RomanNumeral.X);
RomanNumeral.D = new RomanNumeral("D", 500, RomanNumeral.C);
RomanNumeral.M = new RomanNumeral("M", 1000, RomanNumeral.C);
RomanNumeral.MN = new RomanNumeral("MN", 4000, RomanNumeral.D);
RomanNumeral.N = new RomanNumeral("N", 5000, RomanNumeral.D);
RomanNumeral.All = [
RomanNumeral.N,
RomanNumeral.MN,
RomanNumeral.M,
RomanNumeral.D,
RomanNumeral.C,
RomanNumeral.L,
RomanNumeral.X,
RomanNumeral.V,
RomanNumeral.I
];
return RomanNumeral;
})();
var RomanNumeralConverter = (function () {
function RomanNumeralConverter(arabic) {
this.arabic = arabic;
}
RomanNumeralConverter.prototype.toRomanNumerals = function () {
var romanNumerals = [], num = this.arabic;
while (num > 0) {
var romanNumeral = RomanNumeral.All.filter(function (x) {
return x.matches(num) != null;
}).map(function (x) {
return x.matches(num);
...