File tree 1 file changed +7
-19
lines changed
1 file changed +7
-19
lines changed Original file line number Diff line number Diff line change @@ -165,25 +165,13 @@ function toRomanNumerals(number, lowerCase = false) {
165
165
Number . isInteger ( number ) && number > 0 ,
166
166
"The number should be a positive integer."
167
167
) ;
168
- const romanBuf = [ ] ;
169
- // Thousands
170
- while ( number >= 1000 ) {
171
- number -= 1000 ;
172
- romanBuf . push ( "M" ) ;
173
- }
174
- // Hundreds
175
- let pos = ( number / 100 ) | 0 ;
176
- number %= 100 ;
177
- romanBuf . push ( ROMAN_NUMBER_MAP [ pos ] ) ;
178
- // Tens
179
- pos = ( number / 10 ) | 0 ;
180
- number %= 10 ;
181
- romanBuf . push ( ROMAN_NUMBER_MAP [ 10 + pos ] ) ;
182
- // Ones
183
- romanBuf . push ( ROMAN_NUMBER_MAP [ 20 + number ] ) ; // eslint-disable-line unicorn/no-array-push-push
184
-
185
- const romanStr = romanBuf . join ( "" ) ;
186
- return lowerCase ? romanStr . toLowerCase ( ) : romanStr ;
168
+
169
+ const roman =
170
+ "M" . repeat ( ( number / 1000 ) | 0 ) +
171
+ ROMAN_NUMBER_MAP [ ( ( number % 1000 ) / 100 ) | 0 ] +
172
+ ROMAN_NUMBER_MAP [ 10 + ( ( ( number % 100 ) / 10 ) | 0 ) ] +
173
+ ROMAN_NUMBER_MAP [ 20 + ( number % 10 ) ] ;
174
+ return lowerCase ? roman . toLowerCase ( ) : roman ;
187
175
}
188
176
189
177
// Calculate the base 2 logarithm of the number `x`. This differs from the
You can’t perform that action at this time.
0 commit comments