From 36b9a96f67b2bbcf31d1629326b9dcbd4272e639 Mon Sep 17 00:00:00 2001 From: Ajai-Sharan Date: Sun, 25 Aug 2024 17:27:06 +0530 Subject: [PATCH 1/3] Added feature for Word-Count-Calculator --- .vscode/settings.json | 3 ++ Calculators/Word-Count-Calculator/index.html | 6 +++ Calculators/Word-Count-Calculator/script.js | 44 +++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..f673a71b7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5502 +} \ No newline at end of file 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..e42728272 100644 --- a/Calculators/Word-Count-Calculator/script.js +++ b/Calculators/Word-Count-Calculator/script.js @@ -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; @@ -24,6 +40,12 @@ 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 + " | " + @@ -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, From acae19d35f58a181c5e4c15693f4265384831e10 Mon Sep 17 00:00:00 2001 From: Rakesh Roshan Date: Mon, 2 Dec 2024 22:22:37 +0530 Subject: [PATCH 2/3] Delete .vscode directory --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index f673a71b7..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "liveServer.settings.port": 5502 -} \ No newline at end of file From 073d19b5eb32ab40b95db433cb5980bc59e83876 Mon Sep 17 00:00:00 2001 From: Rakesh Roshan Date: Mon, 2 Dec 2024 22:25:04 +0530 Subject: [PATCH 3/3] 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);