Untitled

a guest
Jun 20th, 2020
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function letterize(input) {
2.     //how many numbers to letterize?
3.     let n = Number(input[0]);
4.
5.     //letterize number 1 to n
6.     for (let index = 1; index <= n; index++) {
7.         let number = Number(input[index]);
8.         let numberAbs = Math.abs(number);
9.
10.         //too small, too large, less than 3 digits
11.         if (number < -999) {
12.             console.log("too small");
13.             continue;
14.         } else if (number > 999) {
15.             console.log("too large");
16.             continue;
17.         } else if (numberAbs < 100) {
18.             continue;
19.         }
20.
21.         //letterize first digit (hundreds)
22.         let hundreds = "";
23.         switch (Math.trunc(numberAbs / 100) % 10) {
24.             case 1: hundreds = "one-hundred"; break;
25.             case 2: hundreds = "two-hundred"; break;
26.             case 3: hundreds = "three-hundred"; break;
27.             case 4: hundreds = "four-hundred"; break;
28.             case 5: hundreds = "five-hundred"; break;
29.             case 6: hundreds = "six-hundred"; break;
30.             case 7: hundreds = "seven-hundred"; break;
31.             case 8: hundreds = "eight-hundred"; break;
32.             case 9: hundreds = "nine-hundred"; break;
33.         }
34.         //add " and" if second and third digit != 0
35.         if (numberAbs % 100 != 0) {
36.             hundreds = hundreds + " and";
37.         }
38.
39.         //letterize second digit (tens)
40.         let tens = "";
41.         switch (Math.trunc(numberAbs / 10) % 10) {
42.             case 1:
43.                 switch (number % 100) {
44.                     case 10: tens = " ten"; break;
45.                     case 11: tens = " eleven"; break;
46.                     case 12: tens = " twelve"; break;
47.                     case 13: tens = " thirteen"; break;
48.                     case 14: tens = " fourteen"; break;
49.                     case 15: tens = " fifteen"; break;
50.                     case 16: tens = " sixteen"; break;
51.                     case 17: tens = " seventeen"; break;
52.                     case 18: tens = " eighteen"; break;
53.                     case 19: tens = " nineteen"; break;
54.                 }
55.                 break;
56.             case 2: tens = " twenty"; break;
57.             case 3: tens = " thirty"; break;
58.             case 4: tens = " fourty"; break;
59.             case 5: tens = " fifty"; break;
60.             case 6: tens = " sixty"; break;
61.             case 7: tens = " seventy"; break;
62.             case 8: tens = " eighty"; break;
63.             case 9: tens = " ninety"; break;
64.         }
65.
66.         //letterize third digit (ones) if second digit != 1
67.         let ones = "";
68.         if (Math.trunc(numberAbs / 10) % 10 != 1) {
69.             switch (numberAbs % 10) {
70.                 case 1: ones = " one"; break;
71.                 case 2: ones = " two"; break;
72.                 case 3: ones = " three"; break;
73.                 case 4: ones = " four"; break;
74.                 case 5: ones = " five"; break;
75.                 case 6: ones = " six"; break;
76.                 case 7: ones = " seven"; break;
77.                 case 8: ones = " eight"; break;
78.                 case 9: ones = " nine"; break;
79.             }
80.         }
81.
82.         //concatenate hundreds, tens and ones
83.         let letterizedNumber = hundreds + tens + ones;
84.         //add "minus " for negative numbers
85.         if (number < 0) {
86.             letterizedNumber = "minus " + letterizedNumber;
87.         }
88.         //print letterized number
89.         console.log(letterizedNumber);
90.     }
91. }