Skip to content

Commit

Permalink
Added feature for Word-Count-Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajai-Sharan committed Aug 25, 2024
1 parent 8bb7f02 commit 36b9a96
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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

0 comments on commit 36b9a96

Please sign in to comment.