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 new results in Word Count Calculator #578

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 3 additions & 1 deletion Calculators/Word-Count-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
<p id="result">
<span class="total-words">Total words: 0</span> |
<span class="unique-words">Unique words: 0</span> |
<span class='shortest-words'>Shortest words: 0</span> |
<span class='longest-words'>Longest words: 0</span> |
<span class="average-length">Average Word Length: 0.00 characters</span>
</p>
<button onclick="exportData()">Export Data</button>
</div>

<script src="script.js"></script>
</body>
</html>
</html>
27 changes: 26 additions & 1 deletion Calculators/Word-Count-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@ function countWords() {
var wordsArray = text.split(/\s+/).filter(function (word) {
return word.length > 0;
});

// Calculate total words
var totalWords = wordsArray.length;

// Calculate unique words
var uniqueWords = countUniqueWords(wordsArray);

// Calculate maximum and minimum words count
var shortest_count = shortestWord(wordsArray).length;
var longest_count = longestWord(wordsArray).length;

// Calculate average word length
var averageWordLength = calculateAverageWordLength(wordsArray);

document.getElementById("result").innerHTML =
"<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++) {
if(minimum.length > wordsArray[i].length)
minimum = wordsArray[i];
}
return minimum;
}

function longestWord(wordsArray) {
let maximum = wordsArray[0]
for( let i = 0; i < wordsArray.length; i++) {
if(maximum.length < wordsArray[i].length)
maximum = wordsArray[i];
}
return maximum;
}

function countUniqueWords(wordsArray) {

Expand Down Expand Up @@ -57,6 +80,8 @@ function exportData() {
var data = {
text: text,
totalWords: wordsArray.length,
shortestWord: shortestWord(wordsArray).length,
longestWord: longestWord(wordsArray).length,
uniqueWords: countUniqueWords(wordsArray),
averageWordLength: calculateAverageWordLength(wordsArray).toFixed(2)
};
Expand Down
Loading