Created
August 11, 2016 04:52
-
-
Save leobetosouza/b2429fe0a9a064beffd20be863519695 to your computer and use it in GitHub Desktop.
Convert any number between 1 and 3999 to roman using JS
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 mapa =[ | |
[1000, { capital : 'M', condition : gt, transform : repeat }], | |
[900, { capital :'CM', condition : gt, transform : difference }], | |
[500, { capital : 'D', condition : gt, transform : difference }], | |
[400, { capital : 'CD', condition : gt, transform : difference }], | |
[100, { capital : 'C', condition : gt, transform : repeat }], | |
[90, { capital : 'XC', condition : gt, transform : difference }], | |
[50, { capital : 'L', condition : gt, transform : difference }], | |
[40, { capital : 'XL', condition : gt, transform : difference }], | |
[10, { capital : 'X', condition : gt, transform : repeat }], | |
[9, { capital : 'IX', condition : eq }], | |
[5, { capital :'V', condition : gt, transform : difference }], | |
[4, { capital : 'IV', condition : eq }], | |
[1, { capital : 'I', condition : gt, transform : repeat }] | |
]; | |
function gt (a, b) { return a >= b } | |
function eq (a, b) { return a === b } | |
function repeat (letter, base, number) { | |
return letter.repeat(Math.floor(number / base)) + to_roman(number % base); | |
} | |
function difference (letter, base, number) { | |
return letter + to_roman(number - base); | |
} | |
function to_roman(number) { | |
if (number < 0 || number > 3999) throw new Error('Não convertemos esse número') | |
for (var [x, obj] of mapa) | |
if (obj.condition(number, x)) | |
return obj.transform ? obj.transform(obj.capital, x, number) : obj.capital | |
return '' | |
} | |
exports.to_roman = to_roman; |
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 assert = require ('assert'), | |
to_roman = require('./roman').to_roman; | |
describe('Números romanos', function() { | |
it('transforma 1', function () { | |
assert.equal(to_roman(1), 'I') | |
}) | |
it('transforma 2', function () { | |
assert.equal(to_roman(2), 'II') | |
}) | |
it('transforma 3', function () { | |
assert.equal(to_roman(3), 'III') | |
}) | |
it('transforma 4', function () { | |
assert.equal(to_roman(4), 'IV') | |
}) | |
it('transforma 5', function () { | |
assert.equal(to_roman(5), 'V') | |
}) | |
it('transforma 10', function () { | |
assert.equal(to_roman(10), 'X') | |
}) | |
it('transforma 20', function () { | |
assert.equal(to_roman(20), 'XX') | |
}) | |
it('transforma 30', function () { | |
assert.equal(to_roman(30), 'XXX') | |
}) | |
it('transforma 9', function () { | |
assert.equal(to_roman(9), 'IX') | |
}) | |
it('transforma 11', function () { | |
assert.equal(to_roman(11), 'XI') | |
}) | |
it('transforma 6', function () { | |
assert.equal(to_roman(6), 'VI') | |
}) | |
it('transforma 7', function () { | |
assert.equal(to_roman(7), 'VII') | |
}) | |
it('transforma 8', function () { | |
assert.equal(to_roman(7), 'VII') | |
}) | |
it('transforma 39', function () { | |
assert.equal(to_roman(39), 'XXXIX') | |
}) | |
it('transforma 34', function () { | |
assert.equal(to_roman(34), 'XXXIV') | |
}) | |
it('transforma 40', function () { | |
assert.equal(to_roman(40), 'XL') | |
}) | |
it('transforma 50', function () { | |
assert.equal(to_roman(50), 'L') | |
}) | |
it('transforma 51', function () { | |
assert.equal(to_roman(51), 'LI') | |
}) | |
it('transforma 41', function () { | |
assert.equal(to_roman(41), 'XLI') | |
}) | |
it('transforma 100', function () { | |
assert.equal(to_roman(100), 'C') | |
}) | |
it('transforma 500', function () { | |
assert.equal(to_roman(500), 'D') | |
}) | |
it('transforma 148', function () { | |
assert.equal(to_roman(148), 'CXLVIII') | |
}) | |
it('transforma 90', function () { | |
assert.equal(to_roman(90), 'XC') | |
}) | |
it('transforma 91', function () { | |
assert.equal(to_roman(91), 'XCI') | |
}) | |
it('transforma 550', function () { | |
assert.equal(to_roman(550), 'DL') | |
}) | |
it('transforma 55', function () { | |
assert.equal(to_roman(55), 'LV') | |
}) | |
it('transforma 445', function () { | |
assert.equal(to_roman(445), 'CDXLV') | |
}) | |
it('transforma 1001', function () { | |
assert.equal(to_roman(1001), 'MI') | |
}) | |
it('transforma 3001', function () { | |
assert.equal(to_roman(3001), 'MMMI') | |
}) | |
it('transforma 3999', function () { | |
assert.equal(to_roman(3999), 'MMMCMXCIX') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment