-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumbersToLetters.js
More file actions
92 lines (74 loc) · 3.85 KB
/
numbersToLetters.js
File metadata and controls
92 lines (74 loc) · 3.85 KB
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
function parseText(userInput){
var userInputArray = userInput.split(' '); // take user input and create array
var pattern = new RegExp("^['\"]?[0-9]+[,;:!?./'\"\n\-]?$"); // numbers only, no other characters (update for decimal numbers later?)
for (var i = 0, n = userInputArray.length; i < n; i++) {
if(pattern.test(userInputArray[i])){
userInputArray[i] = numbersToLetters(userInputArray[i]); // call numbers to letters conversion function and store its return in array
}
}
var userOut = userInputArray.join(' '); // convert array back to string with space between each element
if(document.getElementById('output') !== null){
output.innerHTML = ''; // if there is content in the p#output element, empty it
}
else{
var outPut = document.createElement('p'); // if there is no content, then create the element and add to the body
outPut.id = "output"; // assign id "output"
document.getElementById('outdiv').appendChild(outPut); // append "output" to "outdiv"
}
var textOut = document.createTextNode(userOut); // create text to output
output.appendChild(textOut); // add output text to document at p#output
}
function numbersToLetters(numb){
var punctAfter = null, // punctuation is null by default
punctBefore = null;
var numbArray = numb.split(''); // split number into array
if(isNaN(numbArray[numbArray.length - 1])){
punctAfter = numbArray[numbArray.length - 1]; // if the last element in the array is not a number, store in punctuation after number
numbArray.pop(); // delete last element from array
}
if(isNaN(numbArray[0])){
punctBefore = numbArray[0]; // if the first element in the array is not a number, store in punctuation before number
numbArray.shift(); // delete first element from array
}
numb = numbArray.join(''); // convert array back to string
var onesToLetters = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'],
tensToLetters = ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'];
var ones = numbArray[numbArray.length - 1], // numb % 10,
tens = numbArray[numbArray.length - 2], // (numb % 100 - numb % 10) / 10,
hundreds = numbArray[numbArray.length - 3]; // (numb % 1000 - numb % 100) / 100;
var numbToLet;
numb = parseInt(numb, 10); // verify number is base ten, remove unwanted 0's (???) and punctuation
if(numb < 999 && numb > 0){
if(hundreds > 0){
numbToLet = onesToLetters[hundreds] + ' hundred'; // if the hundreds column has more than 0, print number and hundred
}
else{
numbToLet = ''; // otherwise variable stays empty
}
if(tens > 1){
numbToLet += (hundreds > 0 ? ' ' : '') + tensToLetters[tens] + (ones > 0 ? '-' + onesToLetters[ones] : ''); // if the tens column has value of 2 or more, print corresponding word ('twenty' - 'ninety') and if ones column is greater than 0, print hyphen + word ('one' - 'nine')
}
else if(tens > 0){
numbToLet += (hundreds > 0 ? ' ' : '') + onesToLetters[tens + '' + ones]; // if the tens column is 1, print words 'ten' - 'nineteen'
}
else{
numbToLet += (hundreds > 0 ? ' ' : '') + onesToLetters[ones]; // if the tens column is 0, print just word 'one' - 'nine' (this line added to avoid undefined 'tens')
}
}
else if(numb == 0){
numbToLet = "zero"; // if the number is 0, return "zero"
}
else{
numbToLet = numb; // if the number is outside of 0-999 range, return as is
}
if(punctAfter !== null){
numbToLet += punctAfter; // if punctuation after number is not null, concatenate after number
}
if(punctBefore !== null){
numbToLet = punctBefore + numbToLet; // if punctuation before number is not null concatenate before number
}
return numbToLet;
}
inputButton.addEventListener('click', function(){
parseText(document.getElementById("myTextarea").value); // call parsing function with user input as value
});