Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
roman-numerals-converter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Illia Baida
roman-numerals-converter
Commits
f36eae6d
Commit
f36eae6d
authored
Mar 30, 2020
by
Illia Baida
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Roman to Decimal (ready to test)
parent
8f4a9598
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
21 deletions
+61
-21
App.css
src/App.css
+5
-0
App.js
src/App.js
+22
-13
converter.js
src/helper/converter.js
+34
-8
No files found.
src/App.css
View file @
f36eae6d
...
...
@@ -2,6 +2,11 @@
text-align
:
center
;
}
.App
input
{
font-size
:
14px
;
width
:
250px
;
}
.App-logo
{
height
:
10vmin
;
pointer-events
:
none
;
...
...
src/App.js
View file @
f36eae6d
import
React
,
{
useEffect
,
useCallback
,
useState
,
useMemo
}
from
"react"
;
import
logo
from
"./logo.svg"
;
import
"./App.css"
;
import
Converter
from
'./helper/converter'
;
import
Converter
from
"./helper/converter"
;
const
Inputs
=
{
toDecimal
:
"toDecimal"
,
toRoman
:
"toRoman"
};
...
...
@@ -27,27 +27,36 @@ function App() {
},
[
toDecimalValue
,
toRomanValue
]);
// convert values
const
converted
=
useMemo
(()
=>
{
const
converted
Decimal
=
useMemo
(()
=>
{
const
romanToBeConverted
=
toDecimalValue
;
return
Converter
.
fromRoman
(
romanToBeConverted
);
},
[
toDecimalValue
]);
const
convertedRoman
=
useMemo
(()
=>
{
const
decimalToBeConverted
=
toRomanValue
;
return
{
toDecimal
:
Converter
.
fromRoman
(
romanToBeConverted
),
toRoman
:
Converter
.
toRoman
(
decimalToBeConverted
)
};
},
[
toDecimalValue
,
toRomanValue
]);
return
Converter
.
toRoman
(
decimalToBeConverted
);
},
[
toRomanValue
]);
return
(
<
div
className
=
"App"
>
<
header
className
=
"App-header"
>
<
img
src
=
{
logo
}
className
=
"App-logo"
alt
=
"logo"
/>
<
p
>
Available
range
:
from
1
to
3999
<
/p
>
<
p
>
{
`Available range: from
${
Converter
.
decimalLimit
[
0
]}
to
${
Converter
.
decimalLimit
[
1
]}
`
}
<
/p
>
<
h3
>
Converter
:
Roman
to
Decimal
<
/h3
>
<
input
name
=
{
Inputs
.
toDecimal
}
onChange
=
{
handleChange
}
/
>
<
span
>
Value
is
:
{
converted
.
toDecimal
}
<
/span
>
<
input
name
=
{
Inputs
.
toDecimal
}
onChange
=
{
handleChange
}
placeholder
=
"Enter Roman number here"
/>
<
span
>
Decimal
value
is
:
{
convertedDecimal
}
<
/span
>
<
span
>---<
/span
>
<
h3
>
Converter
:
Decimal
to
Roman
<
/h3
>
<
input
name
=
{
Inputs
.
toRoman
}
onChange
=
{
handleChange
}
/
>
<
span
>
Value
is
:
{
converted
.
toRoman
}
<
/span
>
<
input
name
=
{
Inputs
.
toRoman
}
onChange
=
{
handleChange
}
placeholder
=
"Enter Decimal number here"
/>
<
span
>
Roman
value
is
:
{
convertedRoman
}
<
/span
>
<
/header
>
<
/div
>
);
...
...
src/helper/converter.js
View file @
f36eae6d
export
default
class
Converter
{
// NOTE: there is possible to extend Dictionary: first Add new <letter> : <value> pair then update decimalLimit array
static
decimalLimit
=
[
1
,
3999
];
static
Dictionary
=
{
I
:
1
,
V
:
5
,
...
...
@@ -6,7 +8,9 @@ export default class Converter {
L
:
50
,
C
:
100
,
D
:
500
,
M
:
1000
M
:
1000
,
// K: 5000, // extend Dictionary by adding at least 2 new pairs
// F: 10000,
};
static
_dictionaryValues
=
Object
.
values
(
this
.
Dictionary
).
reverse
();
...
...
@@ -61,8 +65,6 @@ export default class Converter {
return
dictionary
;
}
static
_formatExpression
()
{}
static
_multiplyString
(
str
,
times
)
{
let
res
=
""
;
for
(
let
i
=
0
;
i
<
times
;
i
++
)
{
...
...
@@ -74,7 +76,11 @@ export default class Converter {
static
toRoman
(
decimalValue
)
{
let
romanExpression
=
""
;
this
.
_decimalToPropagate
=
decimalValue
;
this
.
_decimalToPropagate
=
+
decimalValue
;
if
(
this
.
_decimalToPropagate
<
this
.
decimalLimit
[
0
]
||
this
.
_decimalToPropagate
>
this
.
decimalLimit
[
1
]){
return
'[Validation error: invalid range]'
}
const
calculatedDictionary
=
this
.
_dictionaryNames
.
reduce
(
(
dictionary
,
sign
)
=>
this
.
_extractMultiple
(
sign
,
dictionary
),
...
...
@@ -149,20 +155,40 @@ export default class Converter {
let
result
=
0
;
const
signs
=
romanExpression
.
split
(
""
);
const
validated
=
signs
.
every
(
s
=>
this
.
_validateSign
(
s
));
if
(
!
validated
){
return
'[Validation error]'
}
for
(
let
seqIndex
=
0
;
seqIndex
<
signs
.
length
;
)
{
const
currentSign
=
signs
[
seqIndex
];
const
upperDictionarySign
=
this
.
_dictionaryNames
[
this
.
_dictionaryNames
.
indexOf
(
currentSign
)
-
1
];
const
upperDictionaryDecimalGroupSign
=
this
.
_dictionaryNames
[
this
.
_dictionaryNames
.
indexOf
(
currentSign
)
-
2
];
if
(
upperDictionarySign
===
signs
[
seqIndex
+
1
])
{
// records like I
X CD CM
XL
if
(
upperDictionarySign
&&
upperDictionarySign
===
signs
[
seqIndex
+
1
])
{
// records like I
V CD
XL
const
representChunk
=
this
.
Dictionary
[
upperDictionarySign
]
-
this
.
Dictionary
[
currentSign
];
result
+=
representChunk
;
seqIndex
++
;
seqIndex
+=
2
;
}
else
if
(
upperDictionarySign
&&
this
.
_isSignDescribesDecimalGroup
(
currentSign
)
&&
upperDictionaryDecimalGroupSign
===
signs
[
seqIndex
+
1
]
)
{
// records like IX CM XC
const
representChunk
=
this
.
Dictionary
[
upperDictionaryDecimalGroupSign
]
-
this
.
Dictionary
[
currentSign
];
result
+=
representChunk
;
seqIndex
+=
2
;
}
else
{
// records like CCC XX III
// records like CCC XX III
X I
const
timesToMultiply
=
this
.
_extractSeqSameSigns
(
signs
,
currentSign
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment