-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberSpellingConverter.js
280 lines (258 loc) · 6.77 KB
/
NumberSpellingConverter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Dictionaries:
const zero = "zero";
const ones = {
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine"
};
const teens = {
"1": "eleven",
"2": "twelve",
"3": "thirteen",
"4": "fourteen",
"5": "fifteen",
"6": "sixteen",
"7": "seventeen",
"8": "eighteen",
"9": "nineteen"
};
const tens = {
"1": "ten",
"2": "twenty",
"3": "thirty",
"4": "forty",
"5": "fifty",
"6": "sixty",
"7": "seventy",
"8": "eighty",
"9": "ninety"
};
const ranges = {
"1": " thousand ",
"2": " million ",
"3": " billion ",
"4": " trillion ",
"5": " quadrillion "
};
const decimalRanges = {
"1": "tenths",
"2": "hundredths",
"3": "thousandths",
"4": "ten-thousandths"
};
const separate = arr => {
const newArr = [];
const arrCopy = arr;
const separator = arr => {
if (arr.length <= 3) {
return newArr.unshift(arr);
}
const tempArr = arr.splice(-3, 3);
newArr.unshift(tempArr);
separator(arr);
};
separator(arrCopy);
return newArr;
};
function adjustDecimalToPrecision(number, precision) {
try {
let numberCopy = number.toString(10);
const numberCopyArray = numberCopy.split(".");
typeof precision !== "number" && numberCopyArray.length > 1
? (precisionCopy = numberCopyArray[1].length)
: (precisionCopy = precision);
numberCopy = Number(numberCopy).toFixed(precisionCopy);
const splitFinalNumber = numberCopy.split(".");
let int = Number(splitFinalNumber[0]);
let dec = Number(splitFinalNumber[1]);
return [int, dec, precisionCopy];
} catch (err) {
return err.message;
}
}
const useDecimalRange = num => {
switch (num) {
case 1:
case 2:
case 3:
case 4:
return decimalRanges[num];
default:
break;
}
};
const baseConvert = arr => {
const hundred = arr[0];
const ten = arr[1];
const one = arr[2];
let converted = [];
if (hundred === 0) {
if (ten === 0) {
return (converted = converted.concat(ones[one]));
} else {
if (ten === 1 && one > 0) {
return (converted = converted.concat(teens[one]));
}
if (ten === 1 && one === 0) {
return (converted = converted.concat(tens[ten]));
}
if (ten > 1 && one === 0) {
return (converted = converted.concat(tens[ten]));
}
if (ten > 1 && one > 0) {
return (converted = converted.concat(tens[ten] + " " + ones[one]));
}
}
}
if (hundred > 0 && ten === 0) {
if (one === 0) {
return converted.concat(ones[hundred] + " hundred");
} else {
return (converted = converted.concat(
ones[hundred] + " hundred and " + ones[one]
));
}
}
if (hundred > 0 && ten === 1) {
if (one === 0) {
return (converted = converted.concat(
ones[hundred] + " hundred and " + tens[ten]
));
} else {
return (converted = converted.concat(
ones[hundred] + " hundred and " + teens[one]
));
}
}
if (hundred > 0 && ten > 1) {
if (one === 0) {
return (converted = converted.concat(
ones[hundred] + " hundred and " + tens[ten]
));
} else {
return (converted = converted.concat(
ones[hundred] + " hundred and " + tens[ten] + "-" + ones[one]
));
}
}
return converted;
};
const isZero = arr =>
arr.reduce((acc, curr) => {
curr === 0 ? acc : (acc = false);
return acc;
}, true);
const fillUpArrayWithLeadingZeros = arr => {
for (let i = arr.length; i < 3; i++) {
arr.unshift(0);
}
return arr;
};
const numericalSringArrayToNumberArray = arr => {
return arr.map(el => Number(el));
};
const convertInteger = num => {
try {
const numToString = num.toString(10);
if (numToString.length > 15) {
throw new Error(" The number is too large.");
} else {
const numToString = num.toString(10);
const numToStringArray = numToString.split("");
if (numToStringArray.length < 3) {
fillUpArrayWithLeadingZeros(numToStringArray);
}
const numbersArray = numericalSringArrayToNumberArray(numToStringArray);
let numberSplittedIntoRangesArray = separate(numbersArray);
if (numberSplittedIntoRangesArray.length === 1) {
numberSplittedIntoRangesArray = numberSplittedIntoRangesArray.flat();
let converted;
isZero(numberSplittedIntoRangesArray)
? (converted = zero)
: (converted = baseConvert(numberSplittedIntoRangesArray).join(" "));
return converted;
}
numberSplittedIntoRangesArray.reverse();
let converted = numberSplittedIntoRangesArray.reduce(
(acc, curr, index) => {
if (curr.length < 3) {
fillUpArrayWithLeadingZeros(curr);
}
if (index === 0) {
if (curr[0] === 0) {
isZero(curr) ? acc : acc.unshift("and " + baseConvert(curr));
return acc;
}
acc.unshift(baseConvert(curr));
return acc;
}
if (isZero(curr)) {
return acc;
}
acc.unshift(baseConvert(curr) + ranges[index]);
acc = acc.flat();
return acc;
},
[]
);
converted = converted.join(" ");
return converted;
}
} catch (err) {
return err.message;
}
};
const spellNumber = (number, precision) => {
try {
let numberCopy = number.valueOf();
if (typeof numberCopy !== "number") {
throw new Error(" Argument should be a number ");
}
if (Number.isInteger(number)) {
return convertInteger(number);
} else {
numberCopy = numberCopy.toString(10);
}
const numberCopyArray = numberCopy.split(".");
if (numberCopyArray[1] && numberCopyArray[1].length > 4) {
throw new Error(
"Please provide only numbers with up to four decimal digits"
);
} else {
let getNumberArray;
getNumberArray = [...adjustDecimalToPrecision(number, precision)];
const integerPortion = getNumberArray[0];
let decimalPortion = getNumberArray[1];
const precisionCopy = getNumberArray[2];
const spelledNumber =
convertInteger(integerPortion) +
" and " +
convertInteger(decimalPortion) +
" " +
useDecimalRange(precisionCopy);
return spelledNumber;
}
} catch (err) {
return err.message;
}
};
function handleInput() {
const button = document.getElementById("convert");
let result = document.getElementById("result");
function handleClick() {
button.addEventListener("click", e => {
let numberInput = document.getElementById("numberField").value;
numberInput = Number(numberInput);
const spelled = spellNumber(numberInput);
result.innerHTML = spelled;
});
}
button.onclick = handleClick;
}
window.onload = handleInput;