Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View modern_roman_numerals.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
var assert = require("assert")
 
var arabic_for_roman = function(roman) {
var arabic_value_for = {
"I" : 1,
"V" : 5,
"X" : 10,
"L" : 50,
"C" : 100,
"D" : 500,
"M" : 1000,
};
 
var arabic = 0;
 
for (var i = 0; i < roman.length; i++) {
var current = arabic_value_for[roman[i]];
var next = arabic_value_for[roman[i+1]];
if (next > current) {
arabic -= current;
} else {
arabic += current;
}
}
 
return arabic;
}
 
var expected_value = {
"I" : 1,
"II" : 2,
"III" : 3,
"IV" : 4,
"V" : 5,
"VI" : 6,
"IX" : 9,
"X" : 10,
"XV" : 15,
"XX" : 20,
"XXX" : 30,
"L" : 50,
"LXVI" : 66,
"C" : 100,
"D" : 500,
"CMXLVI" : 946,
"M" : 1000,
}
 
describe('arabic_for_roman converter', function() {
Object.keys(expected_value).forEach(function (key) {
it('should return ' + expected_value[key] + ' for ' + key, function() {
assert.equal(expected_value[key], arabic_for_roman(key))
})
})
})
 
// Now depending on the rules you go by, these may be treated as errors.
// This implementation will however work out reasonable values for them.
// {
// "VV" : 10,
// "LL" : 100,
// "DDD" : 1500,
// "CMDCCXLVI" : 1646,
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.