Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add js/ts solutions to lc problem: No.0273 #3378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 3 commits into doocs:main from rain84:feature/lc-0273
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,58 @@ public class Solution {
}
```

#### TypeScript

```ts
function numberToWords(num: number): string {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x: number): string => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

#### JavaScript

```js
function numberToWords(num) {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x) => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
52 changes: 52 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,58 @@ public class Solution {
}
```

#### TypeScript

```ts
function numberToWords(num: number): string {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x: number): string => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

#### JavaScript

```js
function numberToWords(num) {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x) => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
21 changes: 21 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/Solution.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function numberToWords(num) {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x) => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
21 changes: 21 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/Solution.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function numberToWords(num: number): string {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x: number): string => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}

AltStyle によって変換されたページ (->オリジナル) /