Skip to content

Commit 53ef626

Browse files
committed
feat: add js solution to lc problem: No.0273
1 parent 714aa26 commit 53ef626

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

solution/0200-0299/0273.Integer to English Words/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ function numberToWords(num: number): string {
322322
}
323323
```
324324

325+
#### JavaScript
326+
327+
```js
328+
function numberToWords(num) {
329+
if (num === 0) return 'Zero';
330+
331+
// prettier-ignore
332+
const f = (x) => {
333+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
334+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
335+
let ans = ''
336+
337+
if (x <= 19) ans = dict1[x] ?? ''
338+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
339+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
340+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
341+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
342+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
343+
344+
return ans.trim()
345+
}
346+
347+
return f(num);
348+
}
349+
```
350+
325351
<!-- tabs:end -->
326352
327353
<!-- solution:end -->

solution/0200-0299/0273.Integer to English Words/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,32 @@ function numberToWords(num: number): string {
320320
}
321321
```
322322

323+
#### JavaScript
324+
325+
```js
326+
function numberToWords(num) {
327+
if (num === 0) return 'Zero';
328+
329+
// prettier-ignore
330+
const f = (x) => {
331+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
332+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
333+
let ans = ''
334+
335+
if (x <= 19) ans = dict1[x] ?? ''
336+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
337+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
338+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
339+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
340+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
341+
342+
return ans.trim()
343+
}
344+
345+
return f(num);
346+
}
347+
```
348+
323349
<!-- tabs:end -->
324350
325351
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function numberToWords(num) {
2+
if (num === 0) return 'Zero';
3+
4+
// prettier-ignore
5+
const f = (x) => {
6+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
7+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
8+
let ans = ''
9+
10+
if (x <= 19) ans = dict1[x] ?? ''
11+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
12+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
13+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
14+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
15+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
16+
17+
return ans.trim()
18+
}
19+
20+
return f(num);
21+
}

0 commit comments

Comments
 (0)