Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakesh9100 authored Dec 2, 2024
1 parent acae19d commit 073d19b
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions Calculators/Word-Count-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("inputText").addEventListener("input", countWords);
});

Expand All @@ -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;
Expand All @@ -40,18 +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-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 @@ -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 = {
Expand All @@ -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);
Expand Down

0 comments on commit 073d19b

Please sign in to comment.