Created
January 7, 2011 13:19
-
-
Save NigelThorne/769441 to your computer and use it in GitHub Desktop.
Novel solution to the 'Convert Roman Numerals to Integers' 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
String.prototype.fromRoman = function() { | |
maps = new Object(); | |
maps["I"] = 1; | |
maps["V"] = 5; | |
maps["X"] = 10; | |
maps["L"] = 50; | |
maps["C"] = 100; | |
maps["D"] = 500; | |
maps["M"] = 1000; | |
translateToValue = function(c){return maps[c];} | |
next = 0 | |
negateSubtractedValues = function(digitValue){ | |
nextDigitValue = next; | |
next = digitValue; | |
return digitValue < nextDigitValue ? -digitValue : digitValue; | |
} | |
summing = function(a,b){return a + b;} | |
return this | |
.split("") | |
.map(translateToValue) | |
.reverse() | |
.map(negateSubtractedValues) | |
.reduce(summing,0); | |
}; |
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
var vows = require('vows'), | |
assert = require('assert'); | |
vows.describe('String').addBatch({ | |
'A string' : { | |
'that`s empty': { | |
topic: "", | |
'is roman for 0': function (topic) { | |
assert.equal(topic.fromRoman(), 0); | |
} | |
}, | |
'I': { | |
topic: "I", | |
'is roman for 1': function (topic) { | |
assert.equal(topic.fromRoman(), 1); | |
} | |
}, | |
'II': { | |
topic: "II", | |
'is roman for 2': function (topic) { | |
assert.equal(topic.fromRoman(), 2); | |
} | |
}, | |
'III': { | |
topic: "III", | |
'is roman for 3': function (topic) { | |
assert.equal(topic.fromRoman(), 3); | |
} | |
}, | |
'IV': { | |
topic: "IV", | |
'is roman for 4': function (topic) { | |
assert.equal(topic.fromRoman(), 4); | |
} | |
}, | |
'V': { | |
topic: "V", | |
'is roman for 5': function (topic) { | |
assert.equal(topic.fromRoman(), 5); | |
} | |
}, | |
'VI': { | |
topic: "VI", | |
'is roman for 6': function (topic) { | |
assert.equal(topic.fromRoman(), 6); | |
} | |
}, | |
'VII': { | |
topic: "VII", | |
'is roman for 7': function (topic) { | |
assert.equal(topic.fromRoman(), 7); | |
} | |
}, | |
'VIII': { | |
topic: "VIII", | |
'is roman for 8': function (topic) { | |
assert.equal(topic.fromRoman(), 8); | |
} | |
}, | |
'IX': { | |
topic: "IX", | |
'is roman for 9': function (topic) { | |
assert.equal(topic.fromRoman(), 9); | |
} | |
}, | |
'X': { | |
topic: "X", | |
'is roman for 10': function (topic) { | |
assert.equal(topic.fromRoman(), 10); | |
} | |
}, | |
'XI': { | |
topic: "XI", | |
'is roman for 11': function (topic) { | |
assert.equal(topic.fromRoman(), 11); | |
} | |
}, | |
'XII': { | |
topic: "XII", | |
'is roman for 12': function (topic) { | |
assert.equal(topic.fromRoman(), 12); | |
} | |
}, | |
'XIII': { | |
topic: "XIII", | |
'is roman for 13': function (topic) { | |
assert.equal(topic.fromRoman(), 13); | |
} | |
}, | |
'XIV': { | |
topic: "XIV", | |
'is roman for 14': function (topic) { | |
assert.equal(topic.fromRoman(), 14); | |
} | |
}, | |
//... | |
'XX': { | |
topic: "XX", | |
'is roman for 20': function (topic) { | |
assert.equal(topic.fromRoman(), 20); | |
} | |
}, | |
// ... | |
'L': { | |
topic: "L", | |
'is roman for 50': function (topic) { | |
assert.equal(topic.fromRoman(), 50); | |
} | |
}, | |
'XC': { | |
topic: "XC", | |
'is roman for 90': function (topic) { | |
assert.equal(topic.fromRoman(), 90); | |
} | |
}, | |
'XCIX': { | |
topic: "XCIX", | |
'is roman for 99': function (topic) { | |
assert.equal(topic.fromRoman(), 99); | |
} | |
}, | |
} | |
}).export(module) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment