Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 5f961ba + 0bb7942 commit b8fecddCopy full SHA for b8fecdd
1 file changed
Conversions/RomanToDecimal.js
@@ -0,0 +1,38 @@
1
+var values = {
2
+ I: 1,
3
+ V: 5,
4
+ X: 10,
5
+ L: 50,
6
+ C: 100,
7
+ D: 500,
8
+ M: 1000
9
+}
10
+
11
+function romanToDecimal (romanNumber) {
12
+ let prev = ' '
13
14
+ let sum = 0
15
16
+ let newPrev = 0
17
+ for (let i = romanNumber.length - 1; i >= 0; i--) {
18
+ const c = romanNumber.charAt(i)
19
20
+ if (prev !== ' ') {
21
+ newPrev = values[prev] > newPrev ? values[prev] : newPrev
22
+ }
23
24
+ const currentNum = values[c]
25
+ if (currentNum >= newPrev) {
26
+ sum += currentNum
27
+ } else {
28
+ sum -= currentNum
29
30
31
+ prev = c
32
33
+ return sum
34
35
36
+console.log(romanToDecimal('XXIIVV'))
37
+console.log(romanToDecimal('MDCCCIV'))
38
+console.log(romanToDecimal('XXIVI'))
0 commit comments