MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / convertToWords

Function convertToWords

javascript/0273-integer-to-english-words.js:8–72  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

6 */
7
8var convertToWords = function (num) {
9 var belowTwenty = [
10 '',
11 'One',
12 'Two',
13 'Three',
14 'Four',
15 'Five',
16 'Six',
17 'Seven',
18 'Eight',
19 'Nine',
20 'Ten',
21 'Eleven',
22 'Twelve',
23 'Thirteen',
24 'Fourteen',
25 'Fifteen',
26 'Sixteen',
27 'Seventeen',
28 'Eighteen',
29 'Nineteen',
30 ];
31 var belowHundred = [
32 '',
33 'Ten',
34 'Twenty',
35 'Thirty',
36 'Forty',
37 'Fifty',
38 'Sixty',
39 'Seventy',
40 'Eighty',
41 'Ninety',
42 ];
43 var thousands = ['', 'Thousand', 'Million', 'Billion'];
44 var pointer = 0;
45 result = '';
46
47 while (num > 0) {
48 var words = '';
49 reminder = num % 1000;
50 num = Math.floor(num / 1000);
51
52 if (reminder > 0) {
53 if (reminder >= 100) {
54 words += belowTwenty[Math.floor(reminder / 100)] + ' Hundred ';
55 reminder %= 100;
56 }
57
58 if (reminder >= 20) {
59 words += belowHundred[Math.floor(reminder / 10)] + ' ';
60 reminder %= 10;
61 }
62
63 if (reminder > 0) {
64 words += belowTwenty[Math.floor(reminder)] + ' ';
65 }

Callers 1

numberToWordsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected