Commit 8f4a9598 authored by Illia Baida's avatar Illia Baida

Updated Converter

parent 866e77f9
......@@ -124,9 +124,55 @@ export default class Converter {
return romanExpression;
}
// fromRoman logic below
static _extractSeqSameSigns(signs, targetSign, seqIndex) {
let result = 0;
for (let i = 0; i < this._maxDecimalGroupSigns; i++) {
if (signs[seqIndex + i] === targetSign) {
result += 1;
} else {
break;
}
}
if (result > this._maxDecimalGroupSigns) {
console.error(
`Seq row error: should be less than ${this._maxDecimalGroupSigns + 1}`
);
}
return result;
}
static fromRoman(romanExpression) {
let result = 0;
const signs = romanExpression.split('');
const signs = romanExpression.split("");
for (let seqIndex = 0; seqIndex < signs.length; ) {
const currentSign = signs[seqIndex];
const upperDictionarySign = this._dictionaryNames[
this._dictionaryNames.indexOf(currentSign) - 1
];
if (upperDictionarySign === signs[seqIndex + 1]) {
// records like IX CD CM XL
const representChunk =
this.Dictionary[upperDictionarySign] - this.Dictionary[currentSign];
result += representChunk;
seqIndex++;
} else {
// records like CCC XX III
const timesToMultiply = this._extractSeqSameSigns(
signs,
currentSign,
seqIndex
);
const representChunk = this.Dictionary[currentSign] * timesToMultiply;
result += representChunk;
seqIndex += timesToMultiply;
}
}
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment