Created
February 28, 2012 20:38
-
-
Save Pindar/1934952 to your computer and use it in GitHub Desktop.
RomanNumerals
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
KT.namespace("algo"); | |
KT.algo.romanNumberals = (function () { | |
"use strict"; | |
function getKeyValueObject(number, romanNumber) { | |
return { | |
"part": number, | |
"romanNumber": romanNumber | |
}; | |
} | |
return function (number) { | |
var map = [ | |
getKeyValueObject(1000,"M"), | |
getKeyValueObject(900,"CM"), | |
getKeyValueObject(500,"D"), | |
getKeyValueObject(400,"CD"), | |
getKeyValueObject(100,"C"), | |
getKeyValueObject(90,"XC"), | |
getKeyValueObject(50,"L"), | |
getKeyValueObject(40,"XL"), | |
getKeyValueObject(10, "X"), | |
getKeyValueObject(9, "IX"), | |
getKeyValueObject(5, "V"), | |
getKeyValueObject(4, "IV"), | |
getKeyValueObject(1, "I") | |
]; | |
function getPart(value) { | |
var o = new Array(Math.floor(number / value.part) + 1).join(value.romanNumber); | |
number = number % value.part; | |
return o; | |
} | |
return (isNaN(number)) ? "" : map.map(getPart).join(''); | |
}; | |
})(); |
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
(function () { | |
var convert; | |
TestCase("number to roman number", sinon.testCase({ | |
setUp: function () { | |
convert = KT.algo.romanNumberals; | |
}, | |
"test convert is a function": | |
function () { | |
assertFunction(convert); | |
}, | |
"test less then 0": | |
function () { | |
assertEquals("", convert(-1)); | |
}, | |
"test 0": | |
function () { | |
assertEquals("", convert(0)); | |
}, | |
"test not a number": | |
function () { | |
assertEquals("", convert(undefined)); | |
assertEquals("", convert(null)); | |
assertEquals("", convert("foo")); | |
}, | |
"test convert 1,2,3 to I, II, III": | |
function () { | |
assertEquals("I", convert(1)); | |
assertEquals("II", convert(2)); | |
assertEquals("III", convert(3)); | |
assertEquals("VI", convert(6)); | |
assertEquals("VII", convert(7)); | |
assertEquals("VIII", convert(8)); | |
assertEquals("X", convert(10)); | |
assertEquals("XI", convert(11)); | |
assertEquals("XII", convert(12)); | |
assertEquals("XIII", convert(13)); | |
assertEquals("MCMXC", convert(1990)); | |
assertEquals("MMVIII", convert(2008)); | |
}, | |
"test convert 4 to IV, 9 to IX": | |
function () { | |
assertEquals("IV", convert(4)); | |
assertEquals("IX", convert(9)); | |
assertEquals("XIV", convert(14)); | |
}, | |
"test convert 5 to V": | |
function () { | |
assertEquals("V", convert(5)); | |
} | |
})); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment