File tree 2 files changed +38
-1
lines changed
2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } num
3
+ * @return {string }
4
+ */
5
+ var intToRoman = function ( num ) {
6
+ const valueSymbols = [
7
+ [ 1000 , 'M' ] ,
8
+ [ 900 , 'CM' ] ,
9
+ [ 500 , 'D' ] ,
10
+ [ 400 , 'CD' ] ,
11
+ [ 100 , 'C' ] ,
12
+ [ 90 , 'XC' ] ,
13
+ [ 50 , 'L' ] ,
14
+ [ 40 , 'XL' ] ,
15
+ [ 10 , 'X' ] ,
16
+ [ 9 , 'IX' ] ,
17
+ [ 5 , 'V' ] ,
18
+ [ 4 , 'IV' ] ,
19
+ [ 1 , 'I' ] ,
20
+ ]
21
+ const roman = [ ] ;
22
+ for ( const [ value , symbol ] of valueSymbols ) {
23
+ while ( num >= value ) {
24
+ num -= value ;
25
+ roman . push ( symbol ) ;
26
+ }
27
+ if ( num == 0 ) {
28
+ break ;
29
+ }
30
+ }
31
+ return roman . join ( '' ) ;
32
+ } ;
33
+
34
+ intToRoman ( 435 ) ;
Original file line number Diff line number Diff line change 5
5
var romanToInt = function ( s ) {
6
6
const map = {
7
7
I : 1 ,
8
+ IV : 4 ,
8
9
V : 5 ,
9
10
IX : 9 ,
10
11
X : 10 ,
31
32
32
33
console . log ( result ) ;
33
34
return result ;
34
- } ;
35
+ } ;
36
+
37
+ romanToInt ( 'CDXXXV' )
You can’t perform that action at this time.
0 commit comments