-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWord-Counter.js
47 lines (37 loc) · 2.29 KB
/
Word-Counter.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
// Function to update word count, letter count, reading time, speaking time, and paragraph count
function updateWordCount() {
// Get the text from the textarea and remove leading/trailing whitespace
const inputText = document.getElementById('inputText').value.trim();
// Split the text into words, filter out empty strings, and count the number of words
const wordCount = inputText.split(/\s+/).filter(word => word !== '').length;
// Count the number of letters (excluding spaces)
const letterCount = inputText.replace(/\s/g, '').length;
// Count the number of paragraphs based on the number of newline characters
const paragraphCount = inputText.split('\n').filter(para => para.trim() !== '').length;
// Calculate reading time based on an average reading speed of 275 words per minute
const readingTimeMinutes = wordCount / 275;
const readingTimeSeconds = Math.ceil(readingTimeMinutes * 60);
// Calculate speaking time based on an average speaking speed of 180 words per minute
const speakingTimeMinutes = wordCount / 180;
const speakingTimeSeconds = Math.ceil(speakingTimeMinutes * 60);
// Format the time as hours, minutes, and seconds
const formatTime = (minutes, seconds) => {
const hours = Math.floor(minutes / 60);
const remainingMinutes = minutes % 60;
return `${hours > 0 ? hours + " hour" + (hours > 1 ? "s" : "") : ""} ${remainingMinutes > 0 ? remainingMinutes + " minute" + (remainingMinutes > 1 ? "s" : "") : ""} ${seconds > 0 ? seconds + " second" + (seconds > 1 ? "s" : "") : ""}`;
};
// Get the result div
const resultDiv = document.getElementById('result');
// Update the inner HTML of the result div with the word count, letter count, reading time, speaking time, and paragraph count
resultDiv.innerHTML = `
<p>Number of words: ${wordCount}</p>
<p>Number of letters: ${letterCount}</p>
<p>Number of paragraphs: ${paragraphCount}</p>
<p>Reading time: ${formatTime(Math.floor(readingTimeMinutes), readingTimeSeconds)}</p>
<p>Speaking time: ${formatTime(Math.floor(speakingTimeMinutes), speakingTimeSeconds)}</p>
`;
}
// Call the updateWordCount function initially
updateWordCount();
// Attach the updateWordCount function to the input event of the textarea
document.getElementById('inputText').addEventListener('input', updateWordCount);