Commit fde42a4a authored by Illia Baida's avatar Illia Baida

Added Converter

parent d2d0995b
import React, { useEffect, useCallback, useState, useMemo } from "react";
import logo from "./logo.svg";
import "./App.css";
import Converter from './helper/converter';
const Inputs = { toDecimal: "toDecimal", toRoman: "toRoman" };
......@@ -30,8 +31,8 @@ function App() {
const romanToBeConverted = toDecimalValue;
const decimalToBeConverted = toRomanValue;
return {
toDecimal: 499,
toRoman: 'CDXCIX'
toDecimal: Converter.fromRoman(romanToBeConverted),
toRoman: Converter.toRoman(decimalToBeConverted)
};
}, [toDecimalValue, toRomanValue]);
......
export default class Converter {
static Dictionary = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
static _dictionaryValues = Object.values(this.Dictionary).reverse();
static _dictionaryNames = Object.keys(this.Dictionary).reverse();
static _maxDecimalGroupSigns = 3;
static _validateSign(sign) {
return sign in this.Dictionary;
}
static _isSignDescribesDecimalGroup(sign) {
return String(this.Dictionary[sign]).substring(0, 1) === "5";
}
static _extractRomanSign(sign, value) {
if (value / this.Dictionary[sign] >= 1) {
return value - this.Dictionary[sign];
} else {
return false;
}
}
static toRoman(decimalValue) {
let romanExpression = "";
let decimal = decimalValue;
const calculatedDictionary = this._dictionaryNames.reduce(
(dictionary, sign) => {
const extractResult = this._extractRomanSign(sign, decimal);
if (extractResult === 0) {
return dictionary;
}
if (typeof extractResult === "number") {
decimal = extractResult;
dictionary[sign] = dictionary[sign] ? dictionary[sign] + 1 : 1;
}
return dictionary;
},
{}
);
console.log(calculatedDictionary);
return romanExpression;
}
static fromRoman(roman) {}
}
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