From 073d19b5eb32ab40b95db433cb5980bc59e83876 Mon Sep 17 00:00:00 2001 From: Rakesh Roshan Date: Mon, 2 Dec 2024 22:25:04 +0530 Subject: [PATCH] Update script.js --- Calculators/Word-Count-Calculator/script.js | 25 +++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/Calculators/Word-Count-Calculator/script.js b/Calculators/Word-Count-Calculator/script.js index e42728272..26cbcb288 100644 --- a/Calculators/Word-Count-Calculator/script.js +++ b/Calculators/Word-Count-Calculator/script.js @@ -1,4 +1,4 @@ - document.addEventListener('DOMContentLoaded', function () { +document.addEventListener('DOMContentLoaded', function () { document.getElementById("inputText").addEventListener("input", countWords); }); @@ -20,7 +20,6 @@ function countWords() { // 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; @@ -40,18 +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 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++) { @@ -99,19 +99,14 @@ 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 = { @@ -131,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);