File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed
Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Decimal To Roman
3+
4+ This algorithm take decimal number and convert to roman numeral according to standard form (https://en.wikipedia.org/wiki/Roman_numerals#Description)
5+
6+ Algorithm & Explanation : https://www.rapidtables.com/convert/number/how-number-to-roman-numerals.html
7+ */
8+
9+ const values = {
10+ M : 1000 ,
11+ CM : 900 ,
12+ D : 500 ,
13+ CD : 400 ,
14+ C : 100 ,
15+ XC : 90 ,
16+ L : 50 ,
17+ XL : 40 ,
18+ X : 10 ,
19+ IX : 9 ,
20+ V : 5 ,
21+ IV : 4 ,
22+ I : 1
23+ }
24+
25+ const orders = [
26+ 'M' ,
27+ 'CM' ,
28+ 'D' ,
29+ 'CD' ,
30+ 'C' ,
31+ 'XC' ,
32+ 'L' ,
33+ 'XL' ,
34+ 'X' ,
35+ 'IX' ,
36+ 'V' ,
37+ 'IV' ,
38+ 'I'
39+ ]
40+
41+ function decimalToRoman ( num ) {
42+ let roman = ''
43+ for ( var symbol of orders ) {
44+ while ( num >= values [ symbol ] ) {
45+ roman += symbol
46+ num -= values [ symbol ]
47+ }
48+ }
49+ return roman
50+ }
51+
52+ export { decimalToRoman }
Original file line number Diff line number Diff line change 1+ import { decimalToRoman } from '../DecimalToRoman'
2+
3+ describe ( 'decimalToRoman' , ( ) => {
4+ it ( 'expects to return correct roman numeral of given number' , ( ) => {
5+ expect ( decimalToRoman ( 34 ) ) . toBe ( 'XXXIV' )
6+ } )
7+ it ( 'expects to return correct roman numeral of given number' , ( ) => {
8+ expect ( decimalToRoman ( 28 ) ) . toBe ( 'XXVIII' )
9+ } )
10+ it ( 'expects to return correct roman numeral of given number' , ( ) => {
11+ expect ( decimalToRoman ( 2021 ) ) . toBe ( 'MMXXI' )
12+ } )
13+ } )
You can’t perform that action at this time.
0 commit comments