AlgoDaily Solution

1var assert = require('assert');
2
3/**
4 * Convert an integer to its words representation
5 *
6 * @author McShaman (http://stackoverflow.com/users/788657/mcshaman)
7 * @source http://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript
8 */
9function numberToWords(n) {
10  var string = n.toString(),
11    units,
12    tens,
13    scales,
14    start,
15    end,
16    chunks,
17    chunksLen,
18    chunk,
19    ints,
20    i,
21    word,
22    words,
23    and = 'and';
24
25  /* Remove spaces and commas */
26  string = string.replace(/[, ]/g, '');
27
28  /* Is number zero? */
29  if (parseInt(string) === 0) {
30    return 'zero';
31  }
32
33  /* Array of units as words */
34  units = [
35    '',
36    'one',
37    'two',
38    'three',
39    'four',
40    'five',
41    'six',
42    'seven',
43    'eight',
44    'nine',
45    'ten',
46    'eleven',
47    'twelve',
48    'thirteen',
49    'fourteen',
50    'fifteen',
51    'sixteen',
52    'seventeen',
53    'eighteen',
54    'nineteen',
55  ];
56
57  /* Array of tens as words */
58  tens = [
59    '',
60    '',
61    'twenty',
62    'thirty',
63    'forty',
64    'fifty',
65    'sixty',
66    'seventy',
67    'eighty',
68    'ninety',
69  ];
70
71  /* Array of scales as words */
72  scales = [
73    '',
74    'thousand',
75    'million',
76    'billion',
77    'trillion',
78    'quadrillion',
79    'quintillion',
80    'sextillion',
81    'septillion',
82    'octillion',
83    'nonillion',
84    'decillion',
85    'undecillion',
86    'duodecillion',
87    'tredecillion',
88    'quatttuor-decillion',
89    'quindecillion',
90    'sexdecillion',
91    'septen-decillion',
92    'octodecillion',
93    'novemdecillion',
94    'vigintillion',
95    'centillion',
96  ];
97
98  /* Split user arguemnt into 3 digit chunks from right to left */
99  start = string.length;
100  chunks = [];
101  while (start > 0) {
102    end = start;
103    chunks.push(string.slice((start = Math.max(0, start - 3)), end));
104  }
105
106  /* Check if function has enough scale words to be able to stringify the user argument */
107  chunksLen = chunks.length;
108  if (chunksLen > scales.length) {
109    return '';
110  }
111
112  /* Stringify each integer in each chunk */
113  words = [];
114  for (i = 0; i < chunksLen; i++) {
115    chunk = parseInt(chunks[i]);
116
117    if (chunk) {
118      /* Split chunk into array of individual integers */
119      ints = chunks[i].split('').reverse().map(parseFloat);
120
121      /* If tens integer is 1, i.e. 10, then add 10 to units integer */
122      if (ints[1] === 1) {
123        ints[0] += 10;
124      }
125
126      /* Add scale word if chunk is not zero and array item exists */
127      if ((word = scales[i])) {
128        words.push(word);
129      }
130
131      /* Add unit word if array item exists */
132      if ((word = units[ints[0]])) {
133        words.push(word);
134      }
135
136      /* Add tens word if array item exists */
137      if ((word = tens[ints[1]])) {
138        words.push(word);
139      }
140
141      /* Add 'and' string after units or tens integer if: */
142      if (ints[0] || ints[1]) {
143        /* Chunk has a hundreds integer or chunk is the first of multiple chunks */
144        if (ints[2] || (!i && chunksLen)) {
145          words.push(and);
146        }
147      }
148
149      /* Add hundreds word if array item exists */
150      if ((word = units[ints[2]])) {
151        words.push(word + ' hundred');
152      }
153    }
154  }
155
156  return words.reverse().join(' ');
157}
158
159try {
160  assert.equal(numberToWords(1234), 'one thousand two hundred and thirty four');
161
162  console.log(
163    'PASSED: ' +
164      "assert.equal(numberToWords(1234), 'one thousand two hundred and thirty four');"
165  );
166} catch (err) {
167  console.log(err);
168}

Community Solutions

Community solutions are only available for premium users.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.

JAVASCRIPT
OUTPUT
Results will appear here.