Skip to content

Commit 4e60bf5

Browse files
committed
Fixed handling for powers of 10 starting from 1000
1 parent 476d045 commit 4e60bf5

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎LeetCode Solutions/0273_IntegerToEnglishWords_jrga03.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
var numberToWords = function(num) {
66
if ( !num ) return 'Zero';
7+
if ( num > 999999999999 ) return 'Number too high';
78

89
let word = '';
9-
if ( num <= 1000 ) {
10+
if ( num < 1000 ) {
1011
word += convertLessThanAThousandToWords( num );
1112
} else {
1213
let unit;
@@ -30,7 +31,11 @@ var numberToWords = function(num) {
3031
const x = Math.floor( num / multiplier );
3132

3233
word += `${convertLessThanAThousandToWords( x )} ${unit} `;
33-
word += numberToWords( num - ( x * multiplier ) );
34+
35+
const remaining = num - ( x * multiplier );
36+
if ( remaining ) {
37+
word += numberToWords( remaining );
38+
}
3439
}
3540

3641
return word.trim();

0 commit comments

Comments
 (0)