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 all commits
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
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
41 changes: 40 additions & 1 deletion Calculators/Word-Count-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,12 +39,19 @@ 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> | " +
"<span class='longest-words'>Longest words: " + longest_count + "</span> | " +
"<span class='average-length'>Average Word Length: " + averageWordLength.toFixed(2) + " characters</span>";
}

function shortestWord(wordsArray) {
let minimum = wordsArray[0]
for (let i = 0; i < wordsArray.length; i++) {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
Loading