Skip to main content
InterviewHubPRO
Section Overview
Q523

Question 523 of 536

Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 536)

98%
JavaScript & V8 Internals (536 Qs)•Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 536)

How do you convert an integer into full English words with place values in JavaScript?

mid LevelCode ExampleFormsPerformanceHTTP & REST APIs

Unlike digit-by-digit transliteration (1234 → "one two three four"), this problem requires converting a non-negative integer into standard conversational English words with place values (e.g., 1234 → "One Thousand Two Hundred Thirty Four"). This is the classic LeetCode 273 (Integer to English Words) interview question.

Core Algorithmic Strategy: 3-Digit Chunking

English numbers are grouped into chunks of thousands:

  • Billions ($10^9$)
  • Millions ($10^6$)
  • Thousands ($10^3$)
  • Hundreds / Units ($10^0$)

We can write a helper function that translates any 3-digit number ($1$ to $999$) into words, and then append the appropriate chunk suffix ("Thousand", "Million", "Billion").

Production Implementation:

javascript
function numberToWords(num) {
  if (num === 0) return "Zero";

  const belowTwenty = [
    "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
    "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
    "Seventeen", "Eighteen", "Nineteen"
  ];

  const tens = [
    "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
  ];

  const thousands = ["", "Thousand", "Million", "Billion"];

  // Helper function to process any number below 1000
  function helper(n) {
    if (n === 0) return "";
    if (n < 20) return belowTwenty[n] + " ";
    if (n < 100) return tens[Math.floor(n / 10)] + " " + helper(n % 10);
    return belowTwenty[Math.floor(n / 100)] + " Hundred " + helper(n % 100);
  }

  let result = "";
  let i = 0;

  while (num > 0) {
    const chunk = num % 1000;
    if (chunk !== 0) {
      const suffix = thousands[i] ? " " + thousands[i] : "";
      result = helper(chunk).trim() + suffix + (result ? " " + result : "");
    }
    num = Math.floor(num / 1000);
    i++;
  }

  return result.trim();
}

console.log(numberToWords(1234));
// "One Thousand Two Hundred Thirty Four"

console.log(numberToWords(1234567));
// "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

console.log(numberToWords(1000010));
// "One Million Ten"

Key Edge Cases & Talking Points:

  • 0: Must explicitly return "Zero".
  • Teens (10–19): Irregular English naming (Eleven, Twelve, Thirteen) must be looked up directly from a dedicated array rather than combining Ten + unit.
  • Empty chunks: 1,000,010 has a middle chunk of 000 (thousands). The chunk helper must return empty string without adding a phantom "Thousand" word.
  • Complexity:
    • Time Complexity: $\mathcal{O}(1)$ because standard 32-bit signed integers have at most 10 digits ($< 2^{31}-1$).
    • Space Complexity: $\mathcal{O}(1)$ auxiliary memory.