-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (41 loc) · 1.4 KB
/
app.js
File metadata and controls
51 lines (41 loc) · 1.4 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
function calculate(display = true){
//Set initial variables
var inputValue = document.getElementById('inputText').value;
var inputArray = [];
var sentenceArray = [];
var word = '';
var textStats = new Object();
textStats.wordCount = 0;
textStats.sentenceCount = 0;
textStats.spaceCount = 0;
textStats.avgWords = 0;
for (i = 0; i < inputValue.length; i++) {
word = word + inputValue[i];
if( (inputValue[i] === ' ') || (i === inputValue.length - 1) ) {
inputArray.push(word);
word = '';
if ( inputValue[i] === ' ' ) {
textStats.spaceCount++;
}
}
}
sentenceArray = inputValue.match(/[^\.!\?]+[\.!\?]+/g);
textStats.sentenceCount = sentenceArray.length;
textStats.wordCount = inputArray.length;
textStats.avgWords = textStats.wordCount / textStats.sentenceCount;
if ( display === true ){
displayStats(textStats);
}
}
function displayStats(textStats = false, consoleOutput = false) {
if ( textStats === false ){
console.log('Unable to calculate statistics!');
exit();
}
else
{
if ( consoleOutput === true ) {
console.log('Words: ' + textStats.wordCount + ' | Sentences: ' + textStats.sentenceCount + ' | Spaces: ' + textStats.spaceCount + ' | Average W/S: ' + textStats.avgWords);
}
}
}