-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript.js
305 lines (257 loc) · 10.3 KB
/
JavaScript.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Interactive Word Counter
Made By: Krisha Botadara*/
function wordCount() {
// Get the search word from the input field
var SearchWord = document.getElementById("search").value;
// Count various statistics
CountTotalWords();
CountTotalLetters();
CountTotalSentences();
CountEachLetter();
CountPunctuations();
CountCommonWords();
if(SearchWord != ""){ // If a search word is provided, count its occurrences
CountSearchWord();
}
else; // Otherwise, do nothing
}
function CountTotalWords(){
//Counts the total number of words in a entered text
var WordCount = 0;
// Get the text from the input field
var words = document.getElementById("w").value;
var split = words.split(' '); // Split the text into words
// Count non-empty words
for (var i = 0; i < split.length; i++) {
if (split[i] != "") {
WordCount ++;
}
}
// Display the total word count
document.getElementById("Words").innerHTML = "Total words: " + WordCount;
}
function CountTotalLetters(){
//Counts the total number of Characters in a entered text
var LetterCount = 1;
// Get the text from the input field
var words = document.getElementById("w").value;
// Split the text into words
var split = words.split(' ');
// Calculate the total number of characters
LetterCount += words.length - split.length;
// Display the total character count
document.getElementById("Characters").innerHTML = "Total Letters" + ": " + LetterCount;
}
function CountTotalSentences(){
// Counts the total number of sentences in the entered text
var SentenceCount = 0;
// Get the text from the input field
var words = document.getElementById("w").value;
// Count sentence-ending punctuation marks
for (var i = 0; i < words.length; i++){
if (words[i] == "." || words[i] == "!" || words[i] == "?") {
SentenceCount ++;
}
}
// Display the total sentence count
document.getElementById("Sentences").innerHTML = "Sentences: " + SentenceCount;
}
function CountEachLetter(){
// Count vowels and consonants separately
CountVowels();
CountConsonants();
}
function CountVowels(){
// Count the occurrences of each vowel in the text
var words = document.getElementById("w").value.toUpperCase();;
var A = E = I = O = U = 0;
for (var i = 0; i < words.length; i++){
switch(words[i]){
case 'A': A++; break;
case 'E': E++; break;
case 'I': I++; break;
case 'O': O++; break;
case 'U': U++; break;
}
}
// Display the counts of each vowel
document.getElementById("letterA").innerHTML = "A: " + A;
document.getElementById("letterE").innerHTML = "E: " + E;
document.getElementById("letterI").innerHTML = "I: " + I;
document.getElementById("letterO").innerHTML = "O: " + O;
document.getElementById("letterU").innerHTML = "U: " + U;
}
function CountConsonants(){
// Count the occurrences of each consonant in the text
var words = document.getElementById("w").value.toUpperCase();
var B = C = D = F = G = H = J = K = L = M = N = P = Q = R = S = T = V = W = X = Y = Z = 0;
for (var i = 0; i < words.length; i++){
switch(words[i]){
case 'B': B++; break;
case 'C': C++; break;
case 'D': D++; break;
case 'F': F++; break;
case 'G': G++; break;
case 'H': H++; break;
case 'J': J++; break;
case 'K': K++; break;
case 'L': L++; break;
case 'M': M++; break;
case 'N': N++; break;
case 'P': P++; break;
case 'Q': Q++; break;
case 'R': R++; break;
case 'S': S++; break;
case 'T': T++; break;
case 'V': V++; break;
case 'W': W++; break;
case 'X': X++; break;
case 'Y': Y++; break;
case 'Z': Z++; break;
}
}
// Display the counts of each consonant
document.getElementById("letterB").innerHTML = "B: " + B;
document.getElementById("letterC").innerHTML = "C: " + C;
document.getElementById("letterD").innerHTML = "D: " + D;
document.getElementById("letterF").innerHTML = "F: " + F;
document.getElementById("letterG").innerHTML = "G: " + G;
document.getElementById("letterH").innerHTML = "H: " + H;
document.getElementById("letterJ").innerHTML = "J: " + J;
document.getElementById("letterK").innerHTML = "K: " + K;
document.getElementById("letterL").innerHTML = "L: " + L;
document.getElementById("letterM").innerHTML = "M: " + M;
document.getElementById("letterN").innerHTML = "N: " + N;
document.getElementById("letterP").innerHTML = "P: " + P;
document.getElementById("letterQ").innerHTML = "Q: " + Q;
document.getElementById("letterR").innerHTML = "R: " + R;
document.getElementById("letterS").innerHTML = "S: " + S;
document.getElementById("letterT").innerHTML = "T: " + T;
document.getElementById("letterV").innerHTML = "V: " + V;
document.getElementById("letterW").innerHTML = "W: " + W;
document.getElementById("letterX").innerHTML = "X: " + X;
document.getElementById("letterY").innerHTML = "Y: " + Y;
document.getElementById("letterZ").innerHTML = "Z: " + Z;
}
function CountPunctuations(){
// Count the occurrences of each punctuation mark in the text
var words = document.getElementById("w").value.toUpperCase();;
var Exclaimation = Colon = Semicolon = Comma = Question = Period = Hyphen = Apostrophe = DoubleQuotes = Underscore = 0;
for (var i = 0; i < words.length; i++){
switch(words[i]){
case '!': Exclaimation++; break;
case ':': Colon++; break;
case ';': Semicolon++; break;
case ',': Comma++; break;
case '?': Question++; break;
case '-': Hyphen++; break;
case "'": Apostrophe++; break;
case '"': DoubleQuotes++; break;
case '_': Underscore++; break;
case '.': Period++; break;
}
}
// Display the counts of each punctuation mark
document.getElementById("exclaimation").innerHTML = "!> " + Exclaimation;
document.getElementById("colon").innerHTML = ":> " + Colon;
document.getElementById("semicolon").innerHTML = ";> " + Semicolon;
document.getElementById("comma").innerHTML = ",> " + Comma;
document.getElementById("question").innerHTML = "?> " + Question;
document.getElementById("hyphen").innerHTML = "-> " + Hyphen;
document.getElementById("apostrophe").innerHTML = "'> " + Apostrophe;
document.getElementById("doublequotes").innerHTML = '"> ' + DoubleQuotes;
document.getElementById("underscore").innerHTML = "_> " + Underscore;
document.getElementById("period").innerHTML = ".> " + Period;
}
function CountCommonWords(){
// Count the occurrences of common words in the text
var words = document.getElementById("w").value.toLowerCase();;
var The = Of = An = And = It = That = This = Is = In = Am = Are = I = My = We = 0;
var words = words.split(' ');
for (var i = 0; i < words.length; i++){
if (words[i] == 'the') {
The++;
}
else if (words[i] == 'of') {
Of++;
}
else if (words[i] == 'an') {
An++;
}
else if (words[i] == 'and') {
And++;
}
else if (words[i] == 'it') {
It++;
}
else if (words[i] == 'that') {
That++;
}
else if (words[i] == 'this') {
This++;
}
else if (words[i] == 'is') {
Is++;
}
else if (words[i] == 'in') {
In++;
}
else if (words[i] == 'am') {
Am++;
}
else if (words[i] == 'are') {
Are++;
}
else if (words[i] == 'i') {
I++;
}
else if (words[i] == 'my') {
My++;
}
else if (words[i] == 'we') {
We++;
}
}
// Display the counts of each common word
document.getElementById("the").innerHTML = "The: " + The;
document.getElementById("of").innerHTML = "Of: " + Of;
document.getElementById("an").innerHTML = "An: " + An;
document.getElementById("and").innerHTML = "And: " + And;
document.getElementById("it").innerHTML = "It: " + It;
document.getElementById("that").innerHTML = "That: " + That;
document.getElementById("this").innerHTML = "This: " + This;
document.getElementById("is").innerHTML = "Is: " + Is;
document.getElementById("in").innerHTML = "In: " + In;
document.getElementById("am").innerHTML = "Am: " + Am;
document.getElementById("are").innerHTML = "Are: " + Are;
document.getElementById("i").innerHTML = "I: " + I;
document.getElementById("my").innerHTML = "My: " + My;
document.getElementById("we").innerHTML = "We: " + We;
}
function CountSearchWord(){
// Count the occurrences of a specific word in the text
var SearchWord = document.getElementById("search").value.toLowerCase();
var words = document.getElementById("w").value.toLowerCase();;
var Word = 0;
var words = words.split(' ');
for (var i = 0; i < words.length; i++){
// If more than one word is entered, display an error message
if (SearchWord.split(' ').length > 1){
document.getElementById("instruction").innerHTML = "Please enter only 1 word: ";
document.getElementById("CountSearch").innerHTML = "Error!";
break;
}
else{
if (SearchWord === words[i]){
Word++;
}
}
}
// Display the count of the searched word
if (SearchWord.split(' ').length > 1){
document.getElementById("CountSearch").innerHTML = "Error!";
}
else{
document.getElementById("CountSearch").innerHTML = SearchWord + ": " + Word;
}
}