Roman Numeral Translator Using Javascript
I got this to work without using underscore, but as an extra challenge I'm trying to convert Roman Numerals to Arabic numbers using underscore. Below is my attempt. It works, exc
Solution 1:
You could use a concise version with Array#reduce
and a look at the actual value and the next value.
functionparseRoman(s) {
var val = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };
return s.toUpperCase().split('').reduce(function (r, a, i, aa) {
return r + (val[a] < val[aa[i + 1]] ? -val[a] : val[a]);
}, 0);
}
console.log(parseRoman("LXIV")); // 64console.log(parseRoman("IX")) // 9console.log(parseRoman("IV")) // 4console.log(parseRoman("CI")); // 101console.log(parseRoman("MMMMCCL")); // 4250
Solution 2:
The main source of my error was that the way I had defined reduce did not allow me access to the index while iterating. I updated my reduce function, then it worked after a little debugging:
varDIGIT_VALUES = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
var each = function(collection, iterator) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
iterator(collection[i], i, collection);
}
} else {
for (var key in collection) {
iterator(collection[key], key, collection);
}
}
};
var reduce = function(collection, iterator, total) {
if (total == undefined) {
total = collection.shift();
}
each(collection, function(val, i) {
total = iterator(total, val, i);
})
return total;
};
var translateRomanNumeral = function(roman) {
if (typeof(roman) !== 'string') {
returnnull;
}
if (!roman) {
return0;
}
// if it's not in the digit values object, return nullfor (var i = 0; i < roman.length; i++) {
if (!(roman[i] inDIGIT_VALUES)) {
returnnull;
}
}
//with underscore:returnreduce(roman, function(memo, letter, i) {
var num = DIGIT_VALUES[letter];
//console.log(i);//how do you acess the next item in a collection in reduce?var next = DIGIT_VALUES[roman[Number(i) + 1]];
// console.log(Number(i) + 1);// console.log(next);if ( next === undefined || next <= num) {
return memo + num;
//console.log(memo);
}
else {
// var diff = num - prev;// console.log(diff);return memo - num;
// memo = memo + (next - num);
}
// return memo;
}, 0);
};
console.log(translateRomanNumeral("LXIV")); //returns 66 ---> should return 64console.log(translateRomanNumeral("IX")) // should return 9console.log(translateRomanNumeral("IV")) /// should return 4console.log(translateRomanNumeral("CI"));
//working --> returns 101console.log(translateRomanNumeral("MMMMCCL"));
// working ---> returns 4250.//works!
Post a Comment for "Roman Numeral Translator Using Javascript"