Convert a number from digits to words

`A JavaScript function for converting numbers (21) to words (twenty-one). It works for numbers up to 999, but it can be transformed to support numbers in the thousands too.`
``````1.<script type="text/javascript">
2.var SingleDigits = new Array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
3.var DoubleDigits = new Array("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
4.
5.function DigitsToWords(Digits)
6.{
7.        var Words = "";
8.        var St;
9.        if (Digits > 999)
10.        {
11.                return "The number exceeds 999.";
12.        }
13.        if (Digits == 0)
14.        {
15.                return SingleDigits[0];
16.        }
17.        for (var i = 9; i >= 1; i--)
18.        {
19.                if (Digits >= i * 100)
20.                {
21.                        Words += SingleDigits[i];
22.                        St = 1;
23.                        Words += " hundred";
24.                        if (Digits != i * 100) Words += " and ";
25.                        {
26.                                Digits -= i*100;
27.                        }
28.                        i=0;
29.                }
30.        }
31.
32.        for (var i = 9; i >= 2; i--)
33.        {
34.                if (Digits >= i * 10)
35.                {
36.                    Words += (St?DoubleDigits[i-2].toLowerCase():DoubleDigits[i-2]);
37.                        St = 1;
38.                        if (Digits != i * 10) Words += "-";
39.                        {
40.                                Digits -= i*10;
41.                        }
42.                        i=0;
43.                }
44.        }
45.
46.        for (var i = 1; i < 20; i++)
47.        {
48.                if (Digits == i)
49.                {
50.                    Words += (St?SingleDigits[i].toLowerCase():SingleDigits[i]);
51.                }
52.        }
53.        return Words;
54.}
55.
56.alert("Are you over " + DigitsToWords(21) + "?");
57.</script>``````
Nathan
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach