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
8f4a9598
Commit
8f4a9598
authored
Mar 30, 2020
by
Illia Baida
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated Converter
parent
866e77f9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
1 deletion
+47
-1
converter.js
src/helper/converter.js
+47
-1
No files found.
src/helper/converter.js
View file @
8f4a9598
...
...
@@ -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
;
}
...
...
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