Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more features in Word Count Calculator #1860

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5502
}
6 changes: 6 additions & 0 deletions Calculators/Word-Count-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<div class="word-count-container">
<textarea id="inputText" oninput="countWords()" placeholder="Type or paste your text here"></textarea>
<p id="result">
<span class="total-characters-with-space">Total Characters (including space): 0</span> |
<span class="total-characters-without-space">Total Characters (not including space): 0</span> |
<span class="total-alphabets">Total Alphabets: 0</span> |
<span class="total-integers">Total Integers: 0</span> |
<span class="total-specialCharacter">Total Special Characters: 0</span>
<br>
<span class="total-words">Total words: 0</span> |
<span class="unique-words">Unique words: 0</span> |
<span class='shortest-words'>Shortest words: 0</span> |
Expand Down
44 changes: 43 additions & 1 deletion Calculators/Word-Count-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("inputText").addEventListener("input", countWords);
});

function countWords() {
var text = document.getElementById("inputText").value;

// calculate total characters including space
var totalCharacterIncludingSpace = text.length;

// calculate total character not including space
var totalCharacterNotIncludingSpace = text.replace(/\s/g, '').length;

// calculate total Alphabets
var totalAlphabets = text.replace(/[^a-zA-Z]/g, '').length;

// calculate total Integers
var totalIntegers = text.replace(/[^0-9]/g, '').length;

// calculate total special characters
var totalSpecialCharacters = text.replace(/[a-zA-Z0-9\s]/g, '').length;


// Regex to split the Words
var wordsArray = text.split(/\s+/).filter(function (word) {
return word.length > 0;
Expand All @@ -24,6 +40,12 @@ function countWords() {
var averageWordLength = calculateAverageWordLength(wordsArray);

document.getElementById("result").innerHTML =
"<span class='total-characters-with-space'>Total Characters (including space): "+ totalCharacterIncludingSpace +"</span> |" +
"<span class='total-characters-without-space'>Total Characters (not including space): "+totalCharacterNotIncludingSpace+"</span> |"+
"<span class='total-alphabets'>Total Alphabets: "+totalAlphabets+"</span> |"+
"<span class='total-integers'>Total Integers: "+totalIntegers+"</span> |"+
"<span class='total-specialCharacter'>Total Special Characters: "+totalSpecialCharacters+"</span> |"+
"<br>"+
"<span class='total-words'>Total words: " + totalWords + "</span> | " +
"<span class='unique-words'>Unique words: " + uniqueWords + "</span> | " +
"<span class='shortest-words'>Shortest words: " + shortest_count + "</span> | " +
Expand Down Expand Up @@ -77,8 +99,28 @@ function exportData() {
return word.length > 0;
});


var totalCharacterIncludingSpace = text.length;


var totalCharacterNotIncludingSpace = text.replace(/\s/g, '').length;


var totalAlphabets = text.replace(/[^a-zA-Z]/g, '').length;


var totalIntegers = text.replace(/[^0-9]/g, '').length;


var totalSpecialCharacters = text.replace(/[a-zA-Z0-9\s]/g, '').length;

var data = {
text: text,
totalCharactersIncludingSpace: totalCharacterIncludingSpace,
totalCharactersNotIncludingSpace: totalCharacterNotIncludingSpace,
totalAlphabets: totalAlphabets,
totalIntegers: totalIntegers,
totalSpecialCharacters: totalSpecialCharacters,
totalWords: wordsArray.length,
shortestWord: shortestWord(wordsArray).length,
longestWord: longestWord(wordsArray).length,
Expand Down