Skip to content

Commit

Permalink
Merge pull request #1050 from aswinisamantray/extra-features-word-cou…
Browse files Browse the repository at this point in the history
…nter

Longest Word
  • Loading branch information
Anmol-Baranwal authored Jun 17, 2023
2 parents 7e59f11 + 4cc6967 commit b65a4b2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
20 changes: 16 additions & 4 deletions Public/word-count-calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>Word length calculator</title>
<title>Text Analyzer</title>
<meta name="description" content="word length calculator">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>

</head>

Expand All @@ -19,20 +21,30 @@
</div>
</div>
</nav>

<div class="input-group">
<div class="input-group-prepend">

</div>
<textarea class="form-control mx-5 my-5 text-success" id="textcontent" aria-label="Large"></textarea>
<textarea class="form-control mx-5 my-5 text-success" id="textcontent" aria-label="Large" placeholder="Enter text here..."></textarea>
</div>
<div class="container text-secondary">
Count of following parameters is displayed here-
<h2>Your text summary</h2>
<hr style="color: brown; height: 2px;">
<div id="countWord"></div>
<div id="countcharacter"></div>
<div id="countalphabets"></div>

<div id="longestWord"></div>
<div id="longestWordlength"></div>
<!-- <button id="copyButton" class="btn btn-success my-2 mx-2" type="submit" >Copy</button>
<button id="pasteButton" class="btn btn-danger my-2 mx-2" type="submit" >Paste</button> -->
</div>

<div class="search my-2">
<input class="form-control me-2" type="search" placeholder="Enter a word to know its frequency..." id="searchText" aria-label="Search"/>
<button id="searchButton" class="btn btn-primary my-2 mx-2" type="submit" >Search</button>
<div id="frequency"></div>
</div>
<script src="../assets/Js/word-count-calculator.js"></script>

</body>
Expand Down
51 changes: 50 additions & 1 deletion assets/Js/word-count-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,45 @@ function count_alphabets(val){
return 0;
}

function longest(val){

let sentence=val.split(" ");
let longestWord=sentence.reduce(function(a,b){
if(a.length>b.length) return a;
return b;
},"");
return longestWord;
}

function search(val,search){
let sentence=val.split(" ");
let count=0;
if(search=='')
return 0;
for(let i=0;i<sentence.length;i++){
if(sentence[i]==search && sentence[sentence.length-1]!=" ") {
count=count+1;
}
}
return count;
}


var textContent = document.getElementById("textcontent");
var showWordCount = document.getElementById("countWord");
var showcharactercount = document.getElementById("countcharacter");
var showalphabetcount = document.getElementById("countalphabets");
var longestWord=document.getElementById("longestWord");
var longestWordlength=document.getElementById("longestWordlength");
var searchText=document.getElementById("searchText");//Word to be searched
var frequency=document.getElementById("frequency");
var copy=document.getElementById("copyButton");
var paste=document.getElementById("pasteButton");
let f=0;

longestWord.innerHTML=("<br>There is no text.");
textContent.addEventListener("input", function(){

var v = count_word( this.value );
showWordCount.innerHTML = (
"<br>Words: "+ v.words
Expand All @@ -33,4 +67,19 @@ var d = count_alphabets(this.value);
showalphabetcount.innerHTML = (
"<br>Alphabets: "+d
);
}, false);
var l=longest(this.value);
if(l.length===0){
longestWord.innerHTML=("<br>There is no text.");
}
else{
longestWord.innerHTML=("<br>Longest Word: "+l.bold());
}
let text=this.value;
longestWordlength.innerHTML=("<br>Length of "+l+" is: "+l.length);
searchButton.addEventListener("click",function(){
let word=searchText.value;
f=search(text,word);
frequency.innerHTML=("<br>"+word.bold()+" occurs "+f+" times(s) in the text.");
},false);
}, false);

0 comments on commit b65a4b2

Please sign in to comment.