-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (50 loc) · 1.47 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
function getAverageWordLength(tokens){
let totalLength = tokens.join('').length;
return (totalLength/tokens.length).toFixed(2);
}
function countUniqueWords(tokens){
let uniqueWordsNum = new Set(tokens);
return uniqueWordsNum.size;
}
// Tokenize Text
/*
Tokenization is the process of turning a meaningful piece of data,
into a random string of characters called a token
that has no meaningful value if breached.
*/
function tokenizeText(text){
return text.toLowerCase().match(/\b[^d\s]+\b/g);
}
function removeReturns(text) {
return text.replace(/\r?\n|\r/g, "");
}
function reportOnText(text){
// Tokenize input and Compute data points
let tokens = tokenizeText(text);
let numWordsTotal = tokens.length;
let numUniqueWords = countUniqueWords(tokens);
let numWordAverage = getAverageWordLength(tokens);
// Display data in the DOM
let textReport = $('.js-text-report');
textReport.find('.js-word-count').text(numWordsTotal);
textReport.find('.js-unique-word-count').text(numUniqueWords);
textReport.find('.js-average-word-length').text(numWordAverage);
textReport.removeClass('hidden');
}
function onFormSubmit(){
$('.js-text-form').submit(function(event){
event.preventDefault();
// Get user input
let userInput = $(this).find('#user-text').val();
reportOnText(removeReturns(userInput));
});
}
$(function() {
onFormSubmit();
});
//Count Words
//Count Unique Words
//Count Average Word Length
//Tokenize Input
//Report Input
//Action on Form Submission