-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (28 loc) · 1.06 KB
/
Copy pathindex.js
File metadata and controls
33 lines (28 loc) · 1.06 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
function wordConter(str){
str = str.replace(/(^\s*)|(\s*$)/gi,"");//exclude start and end white-space
str = str.replace(/[ ]{2,}/gi," ");//2 or more space to 1
str = str.replace(/\n /g,"\n"); // exclude newline with a start spacing
str = str.replace(/\n/g, " ");
return str.split(' ').filter(function(str){return str!="";}).length;
}
function charCounter(str){
str = str.replace(/(^\s*)|(\s*$)/gi,"");//exclude start and end white-space
str = str.replace(/[ ]{2,}/gi,"");//2 or more space to 1
str = str.replace(/\n /g,""); // exclude newline with a start spacing
str = str.replace(/ /g, "");
str = str.replace(/\n/g, "");
return str.length;
}
function setValue(elementId, value){
document.getElementById(elementId).innerHTML = value;
}
function getValue(elementId){
return document.getElementById(elementId).value;
}
function analyze(){
let text = getValue("text");
console.log(text);
setValue("words",
"Total words: " + wordConter(text));
setValue("chars", "Total character: " + charCounter(text));
}