-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (44 loc) · 1.62 KB
/
Copy pathindex.js
File metadata and controls
59 lines (44 loc) · 1.62 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
let charCount = document.getElementById('char')
let wordCount = document.getElementById('count')
let sentences = document.getElementById('sentences')
let spaces = document.getElementById('spaces')
let punctuation = document.getElementById('Punctuation');
const textArea = document.querySelector('.text')
const Processbtn = document.getElementById('btn');
let UIValues = [charCount, wordCount, sentences, spaces, punctuation];
function init() {
UIValues.forEach(value => value.innerHTML = 0);
}
init();
Processbtn.addEventListener('click', () => {
let text = textArea.value;
charCount.innerHTML = text.length;
wordCount.innerHTML = findWord(text);
sentences.innerHTML = findsentences(text);
spaces.innerHTML = text.split(" ").length - 1;
punctuation.innerHTML = findPunctuation(text)
});
function findWord(text) {
let temptext = text.replace(/[.,!%&*;:'"-()]/g, "");
temptext = temptext.replace(/[\r]/g, "").split(/\n/);
let templist = [];
temptext.forEach(word => templist.push(word.split(" ")));
function extract(arr) {
return arr.reduce((wordList, word) => {
return wordList.concat(Array.isArray(word) ? extract
(word) : word);
}, []);
}
let wordList = extract(templist);
return wordList.filter(char => char != '').length;
}
function findsentences(text) {
const regex = /[\w|\)][.?!](\s|$)/g;
let sencount = text.match(regex);
return sencount ? sencount.length : 0;
}
function findPunctuation(text) {
const regex = /[`.,?;:!-'"(){}]/g;
let puncConst = text.match(regex);
return puncConst ? puncConst.length : 0;
}