Commit c758a898 authored by Illia Baida's avatar Illia Baida

Updated Converter

parent 7d9cf822
......@@ -20,7 +20,7 @@ export default class Converter {
}
static _isSignDescribesDecimalGroup(sign) {
return String(this.Dictionary[sign]).substring(0, 1) === "5";
return String(this.Dictionary[sign]).substring(0, 1) !== "5";
}
static _extractRomanSign(sign, value) {
......@@ -32,7 +32,7 @@ export default class Converter {
value / this.Dictionary[sign] >= 1 &&
(value / upperDictionaryValue < 1 || upperDictionaryValue === undefined)
) {
console.log('extract', sign, value);
console.log("extract", sign, value);
return value - this.Dictionary[sign];
} else {
return false;
......@@ -60,6 +60,17 @@ export default class Converter {
return dictionary;
}
static _formatExpression() {}
static _multiplyString(str, times) {
let res = "";
for (let i = 0; i < times; i++) {
res += str;
}
return res;
}
static toRoman(decimalValue) {
let romanExpression = "";
this._decimalToPropagate = decimalValue;
......@@ -69,7 +80,52 @@ export default class Converter {
{}
);
console.log(calculatedDictionary);
console.log("calculatedDictionary", calculatedDictionary);
const skippedNames = [];
for (let index = 0; index < this._dictionaryNames.length; index++) {
const sign = this._dictionaryNames[index];
if(skippedNames.indexOf(sign) !== -1){
console.log('skipping', sign);
continue;
}
if (sign in calculatedDictionary) {
if (this._isSignDescribesDecimalGroup(sign)) {
if (calculatedDictionary[sign] > this._maxDecimalGroupSigns) {
const upperName = this._dictionaryNames[index - 1];
const result = upperName
? `${sign}${upperName}`
: this._multiplyString(sign, calculatedDictionary[sign]);
romanExpression += result;
} else {
romanExpression += this._multiplyString(
sign,
calculatedDictionary[sign]
);
}
} else {
const lowerName = this._dictionaryNames[index + 1];
const upperName = this._dictionaryNames[index - 1];
if (calculatedDictionary[sign] <= 1) {
if (calculatedDictionary[lowerName] > this._maxDecimalGroupSigns) {
const result = `${lowerName}${upperName}`;
romanExpression += result;
} else {
romanExpression += `${sign}${this._multiplyString(
lowerName,
calculatedDictionary[lowerName]
)}`;
// skip
skippedNames.push(lowerName);
}
} else {
throw new Error("Cannot be greater than 1");
}
}
}
}
return romanExpression;
}
......
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