diff --git a/Calculators/Word-Count-Calculator/index.html b/Calculators/Word-Count-Calculator/index.html index 173bc7c75..9e07e8531 100644 --- a/Calculators/Word-Count-Calculator/index.html +++ b/Calculators/Word-Count-Calculator/index.html @@ -15,6 +15,12 @@

+ Total Characters (including space): 0 | + Total Characters (not including space): 0 | + Total Alphabets: 0 | + Total Integers: 0 | + Total Special Characters: 0 +
Total words: 0 | Unique words: 0 | Shortest words: 0 | diff --git a/Calculators/Word-Count-Calculator/script.js b/Calculators/Word-Count-Calculator/script.js index 7bbe99a50..26cbcb288 100644 --- a/Calculators/Word-Count-Calculator/script.js +++ b/Calculators/Word-Count-Calculator/script.js @@ -5,6 +5,21 @@ document.addEventListener('DOMContentLoaded', function () { 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; @@ -24,12 +39,19 @@ function countWords() { var averageWordLength = calculateAverageWordLength(wordsArray); document.getElementById("result").innerHTML = + "Total Characters (including space): " + totalCharacterIncludingSpace + " |" + + "Total Characters (not including space): " + totalCharacterNotIncludingSpace + " |" + + "Total Alphabets: " + totalAlphabets + " |" + + "Total Integers: " + totalIntegers + " |" + + "Total Special Characters: " + totalSpecialCharacters + " |" + + "
" + "Total words: " + totalWords + " | " + "Unique words: " + uniqueWords + " | " + "Shortest words: " + shortest_count + " | " + "Longest words: " + longest_count + " | " + "Average Word Length: " + averageWordLength.toFixed(2) + " characters"; } + function shortestWord(wordsArray) { let minimum = wordsArray[0] for (let i = 0; i < wordsArray.length; i++) { @@ -77,8 +99,23 @@ 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, @@ -89,7 +126,9 @@ function exportData() { // data to JSON format var jsonData = JSON.stringify(data, null, 2); - var blob = new Blob([jsonData], { type: "application/json" }); + var blob = new Blob([jsonData], { + type: "application/json" + }); var a = document.createElement("a"); var url = URL.createObjectURL(blob);