-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (95 loc) · 3.12 KB
/
app.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
/*
• Word matching
REGEX PATTERN: /[\w]+/gi
• Match only english words
REGEX PATTERN: /([^\u0000-\u007F]|\w)+/gi
• Match english or non-english words
*/
// Define variables
const textarea = document.getElementById("textarea")
const words_element = document.getElementById("word_counter")
const characters_element = document.getElementById("character_counter")
const lines_element = document.getElementById("line_counter")
const clear_button_element = document.getElementById("clear_button")
// Word counter function
function word_counter() {
// Word detection
const regex_pattern = /([^\u0000-\u007F]|\w)+/gi
const word_checker = textarea.value.match(regex_pattern)
// When one or more words detected
if (word_checker !== null) {
// Counting Words
const total_words = word_checker.length;
// Display words count
if (total_words > 1) {
// Display plural count
words_element.innerHTML = "Words: " + total_words
} else {
// Display singular count
words_element.innerHTML = "Word: " + total_words
}
} else {
// When no word detected
words_element.innerHTML = "Word: 0"
}
}
// Character counter function
function character_counter() {
// Counting Characters
const total_characters = textarea.value.length
// Display characters count
if (total_characters > 1) {
// Display plural count
characters_element.innerHTML = "Characters: " + total_characters
} else {
// Display singular count
characters_element.innerHTML = "Character: " + total_characters
}
}
// Line counter function
function line_counter() {
// Line detection
const regex_pattern = /\r?\n|\r/g
const line_checker = textarea.value.match(regex_pattern)
// When one or more lines detected
if (line_checker !== null) {
// Counting lines
const total_lines = line_checker.length + 1;
// Display lines count
if (total_lines > 1) {
// Display plural count
lines_element.innerHTML = "Lines: " + total_lines
} else {
// Display singular count
lines_element.innerHTML = "Line: " + total_lines
}
} else {
// When no line detected
lines_element.innerHTML = "Line: 1"
}
}
// Clear textarea function
function clear_textarea() {
// Empty the textarea element value
textarea.value = ""
// Update word count to zero
words_element.innerHTML = "Word: 0"
// Update character count to zero
characters_element.innerHTML = "Character: 0"
// Update line count to zero
lines_element.innerHTML = "Line: 1"
}
// When the input data changed
textarea.oninput = function () {
// Call word counter function
word_counter()
// Call character counter function
character_counter()
// Call line counter function
line_counter()
}
// When the clear button clicked
clear_button_element.onclick = function () {
// Call clear textarea function
clear_textarea()
}