File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ // PSEUDOCODE
2
+
3
+ /**
4
+ *
5
+ * create a hash map with the value of each synol in the roman numeral
6
+ * initiale the result number with 0
7
+ * iterate the roman string
8
+ * check the current string value in the hash if its less than the next character value
9
+ * if its less than the next, subtract it from the next character. (case of IV)
10
+ *
11
+ */
12
+
13
+ var romanToInt = function ( s ) {
14
+ const romanHash = {
15
+ 'I' : 1 ,
16
+ 'V' : 5 ,
17
+ 'X' : 10 ,
18
+ 'L' : 50 ,
19
+ 'C' : 100 ,
20
+ 'D' : 500 ,
21
+ 'M' : 1000 ,
22
+ } ;
23
+
24
+ let result = 0 ;
25
+
26
+ for ( let i = 0 , len = s . length ; i < len ; i ++ ) {
27
+ const currentVal = romanHash [ s [ i ] ] ;
28
+ const nextCharVal = romanHash [ s [ i + 1 ] ] ;
29
+
30
+ if ( currentVal < nextCharVal ) {
31
+ result += nextCharVal - currentVal ;
32
+ i ++
33
+ } else {
34
+ result += currentVal ;
35
+ }
36
+ }
37
+
38
+ return result ;
39
+ }
You can’t perform that action at this time.
0 commit comments