Skip to content

Instantly share code, notes, and snippets.

@avevlad
Created Oct 25, 2013
Embed
What would you like to do?
var dbg = false;
var obj = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
var one, two = '';
var num = 0;
function output(number) {
if (dbg) console.log(number);
number = number.split('');
number.map(function (val, i) {
number[i] = obj[val];
});
if (dbg) console.log(number);
number.forEach(function (item, i) {
one = number[i];
two = number[i + 1];
if (typeof two !== 'undefined') {
if (dbg) console.log(one, two);
if ((one > two) || (one === two)) {
if (i === 0) {
num = num + one + two;
if (dbg) console.log('сложение ONE', num);
} else {
num = num + two;
if (dbg) console.log('сложение TWO', num);
}
}
if (one < two) {
num = num + (-one) + two;
if (dbg) console.log('вычитание', num);
}
}
});
console.log(num);
num = 0;
oneStep = true;
if (dbg) console.log('\n================\n');
}
output('KI');
output('IV');
output('VIII');
output('IX');
output('XXXI');
output('XLVI');
output('XCIX');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment