Skip to content
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
105 changes: 105 additions & 0 deletions WordCounter/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<title>Word Counter</title>
</head>

<body>
<nav class="navbar navbar-dark bg-dark mb-3">
<div class="container-fluid">
<div class="navbar-brand mb-0 h1 text-center w-100">Word Counter</div>
</div>
</nav>
<div class="container">

<div class="col-xl-8 offset-xl-2 mb-3">

<div class="mb-3">
<label class="form-label">Simply input text to start counting OR</label>
<input class="form-control form-control-sm" type="file" name="file-upload" id="upload-file">

</div>

<div class="row d-flex justify-content-center mb-3">
<div class="col-md-4 mb-2">
<div class="card p-2">
<div class="text-center d-flex align-items-center d-md-block justify-content-between">
<div>
<small>Words</small>
</div>
<div>
<h2 class="m-0" id="total-word-strong">0</h2>
</div>
</div>
</div>
</div>

<div class="col-md-4 mb-2">
<div class="card p-2">
<div class="text-center d-flex align-items-center d-md-block justify-content-between">
<div>
<small>Characters</small>
</div>
<div>
<h2 class="m-0" id="total-characters-strong">0</h2>
</div>
</div>
</div>
</div>

<div class="col-md-4 mb-2">
<div class="card p-2">
<div class="text-center d-flex align-items-center d-md-block justify-content-between">
<div>
<small>Characters without spaces</small>
</div>
<div>
<h2 class="m-0" id="total-char-without-spacestrong">0</h2>
</div>
</div>
</div>
</div>


<div class="col-md-4 mb-2">
<div class="card p-2">
<div class="text-center d-flex align-items-center d-md-block justify-content-between">
<div>
<small>Sentences</small>
</div>
<div>
<h2 class="m-0" id="sentences">0</h2>
</div>
</div>
</div>
</div>

<div class="col-md-4 mb-2">
<div class="card p-2">
<div class="text-center d-flex align-items-center d-md-block justify-content-between">
<div>
<small>Paragraphs</small>
</div>
<div>
<h2 class="m-0" id="paragraphs">0</h2>
</div>
</div>
</div>
</div>
</div>

<div class="form-group">
<textarea class="form-control" placeholder="Type or paste your text" id="input" spellcheck="false" rows="45"></textarea>
</div>
</div>
</div>
<script src="main.js"></script>
</body>

</html>
87 changes: 87 additions & 0 deletions WordCounter/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
function count_words(s) {
s = s.replace(/(^\s*)|(\s*$)/gi, ""); //exclude start and end white-
s = s.replace(/[!@#$%^&*(),.?":{}|<>-]/g, "");
s = s.replace(/\n{1,}/gm, " ");
s = s.replace(/[.]{3,}/g, "");
s = s.replace(/[—]{1,}/g, "");
s = s.replace(/[ ]{2,}/gi, " "); //2 or more space to 1
s = s.replace(/\n /, "\n"); // exclude newline with a start spacing
return s.split(' ').filter(function (str) {
return str != "";
}).length;
}

function count_paragraphs(text) {
text = text.replace(/\n$/gm, ''); //.split(/\n/).length;
text = text.replace(/(^\s*)/gi, ""); // delete start \n
return text.split(/\n/).length;
}

function count_sentences(text) {
return text.trim().split(/[\.\?\!]\s/).length;
}

function count_character_space(s) {
s = s.replace(/(^\s*)|(\s*$)/gi, "");
s = s.replace(/\n /, "\n");
return s.length;
}

function count_character(text) {
return text.replace(/\s+/g, "").length;
}

function insert_data() {
document.querySelector("#total-word-strong").innerText = word;
document.querySelector("#total-characters-strong").innerText = character_space;
document.querySelector("#total-char-without-spacestrong").innerText = character;
document.querySelector("#sentences").innerText = sentences;
document.querySelector("#paragraphs").innerText = paragraphs;
}

function analytic_words(text) {
word = count_words(text);
character_space = count_character_space(text);
character = count_character(text);
sentences = count_sentences(text);
paragraphs = 0;

if (word) {
paragraphs = count_paragraphs(text);
}
if (!word) {
sentences = 0;
}

insert_data();
}


document.addEventListener('DOMContentLoaded', () => {
document.getElementById('upload-file').addEventListener('change', function () {
var fr = new FileReader();
fr.onload = function () {
document.getElementById("input").value = this.result;
cal_text_data_for_show();
}
fr.readAsText(this.files[0]);
});
document.querySelector("#input").addEventListener("keyup", () => {
cal_text_delay_for_show();
})
});

function cal_text_data_for_show() {
diff_words = 0;
text = document.querySelector("#input").value
analytic_words(text);
}

var timeoutdata;

function cal_text_delay_for_show() {
clearTimeout(timeoutdata);
timeoutdata = setTimeout(function () {
cal_text_data_for_show();
}, 300);
}