Good morning! Here's our prompt for today.
Can you convert a number to its text equivalent? This is otherwise known as number spelling.

For example, given 1234, we expect to get "one thousand two hundred and thirty four" back.
Constraints
- The given number will have <=
100digits - Let n be the number of digits
- Expected time complexity :
O(n) - Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxxvar assert = require('assert');​function numberToWords(n) { // convert n to words return n;}​try { assert.equal(numberToWords(1234), 'one thousand two hundred and thirty four');​ console.log( 'PASSED: ' + "assert.equal(numberToWords(1234), 'one thousand two hundred and thirty four');" );} catch (err) { console.log(err);}​We'll now take you through what you need to know.
How do I use this guide?
This problem is deceivingly complicated, so let's take it step and step. Let's use a simple number, like 123 for now.
First, let's convert 123 to a string type to start our operation.
xxxxxxxxxx123 -> "123"Then check if it's zero (because if it is, we can just return "zero").
Next, let's make sure we've defined the building blocks that will make up the final string. They include:
unitslikeone,thirteen, etc.tensvalues liketwenty,thirty, etc.scaleslikethousand, andmillion
Now we account for the various scales. Most of the scaling levels come up every 3 digits. Using this code block, we can get every 3 digits as chunks.

xxxxxxxxxxstart = string.length;chunks = [];while (start > 0) { end = start; chunks.push(string.slice((start = Math.max(0, start - 3)), end));}All it does is start from right to left, grabbing every 3 digits and pushing them into a chunks array.
We then adjust for the scale we're at, and make sure we have enough digits to justify that scale.
Now, here's the key. For each chunk, we process it in this manner:
- Split it into individual integers ("123" becomes [1, 2, 3])
- Add tens word if array item exists
- Add 'and' string after units or tens integer (optional)
- Add hundreds word if array item exists

Complexity of Final Solution
Let n be the number of digits in the input num. We iterate through the string equivalent of num, and examining 3 digits at a time for O(n) linear time complexity. When converting nums to a string of length n, we have O(n) space complexity.
One Pager Cheat Sheet
- You can convert a number to its text equivalent using
number spellinginO(n)time andO(n)space complexity, where n is the number of digits and the number of digits must not exceed100. - We can
convertthenumber123 to astring typeto start our operation. - We need to define
units,tensandscalesfor building the final string, and then divide the number into chunks of 3 digits to account for different scales. - We
iteratethroughnuminO(n)lineartime, andconvertit to astringoflength nforO(n)spacecomplexity.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx}var assert = require('assert');​/** * Convert an integer to its words representation * * @author McShaman (http://stackoverflow.com/users/788657/mcshaman) * @source http://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript */function numberToWords(n) { var string = n.toString(), units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words, and = 'and';​ /* Remove spaces and commas */ string = string.replace(/[, ]/g, '');​ /* Is number zero? */ if (parseInt(string) === 0) { return 'zero'; }​Got more time? Let's keep going.
If you had any problems with this tutorial, check out the main forum thread here.

