How do you convert the digits of a number into words in JavaScript?
A common practical coding interview problem is to convert an integer into its digit-by-digit word representation (e.g., input 1234 → output "one two three four").
1. Array Index Lookup (Fastest & Most Idiomatic)
Because individual decimal digits are strictly bounded between 0 and 9, an array lookup provides instantaneous O(1) access per digit:
function numberToWords(num) {
const words = [
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
];
const isNegative = num < 0;
const absNum = Math.abs(num);
const result = String(absNum)
.split("")
.map(digit => words[Number(digit)])
.join(" ");
return isNegative ? `negative ${result}` : result;
}
console.log(numberToWords(1234)); // "one two three four"
console.log(numberToWords(9874)); // "nine eight seven four"
console.log(numberToWords(0)); // "zero"
console.log(numberToWords(-42)); // "negative four two"
2. Using an ES6 Map
You can also use an explicit ES6 Map data structure to map integer keys to their corresponding string words:
function numberToWordsMap(num) {
const wordMap = new Map([
[0, "zero"],
[1, "one"],
[2, "two"],
[3, "three"],
[4, "four"],
[5, "five"],
[6, "six"],
[7, "seven"],
[8, "eight"],
[9, "nine"]
]);
const isNegative = num < 0;
const result = String(Math.abs(num))
.split("")
.map(digit => wordMap.get(Number(digit)))
.join(" ");
return isNegative ? `negative ${result}` : result;
}
console.log(numberToWordsMap(1234)); // "one two three four"
console.log(numberToWordsMap(9874)); // "nine eight seven four"
3. Pure Math Approach: Without Converting to String (Interview Follow-up)
Interviewers frequently ask candidates to solve this without typecasting to a string (disallowing String(), toString(), or string split methods). You can extract digits from right to left using the modulo operator (% 10) and reduce the integer using division (Math.floor(n / 10)):
function numberToWordsMath(num) {
if (num === 0) return "zero";
const words = [
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
];
let n = Math.abs(num);
const result = [];
while (n > 0) {
const digit = n % 10;
result.push(words[digit]);
n = Math.floor(n / 10);
}
// Modulo extracts digits from right to left, so reverse to preserve original order
const wordString = result.reverse().join(" ");
return num < 0 ? `negative ${wordString}` : wordString;
}
console.log(numberToWordsMath(1234)); // "one two three four"
console.log(numberToWordsMath(9874)); // "nine eight seven four"
4. Handling Floating Point Numbers (Decimals)
If the interviewer extends the prompt to support decimal places (e.g., 12.34 → "one two point three four"):
function floatToWords(num) {
const words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
const isNegative = num < 0;
const result = String(Math.abs(num))
.split("")
.map(char => (char === "." ? "point" : words[Number(char)]))
.join(" ");
return isNegative ? `negative ${result}` : result;
}
console.log(floatToWords(12.34)); // "one two point three four"
Key Edge Cases & Interview Talking Points:
- Zero Handling: Input
0must return"zero"rather than an empty string. - Negative Integers: Check if
num < 0and prefix with"negative ". - Digit-by-digit vs. Place-value Words: Clarify whether the interviewer wants digit-by-digit translation (
1234→"one two three four") or full natural English place-value words (1234→"One Thousand Two Hundred Thirty Four"as in LeetCode 273). - Complexity:
- Time Complexity: $\mathcal{O}(d)$, where $d$ is the number of digits in the integer ($\approx \lfloor \log_{10} N \rfloor + 1$).
- Space Complexity: $\mathcal{O}(d)$ to store the mapped string tokens.