Find live frequency of Character in string using Java Script.

In this blog i am going to show you a simple program where you find frequency of character in a string using JavaScript.

Note:- Before starts this simple demo i believe that you must have basic knowledge on JavaScript.

Let start;

To do this demo first we need to design a simple web page where we have one textbox where we need to enter some text and we find the frequency accordingly.

In this example bellow all the functionality are defined inside JavaScript function called as “GetFrequency()

 

<html>
<head>
<meta name=”viewport” content=”width=device-width” />
<title>TestKey</title>
</head>
<body>

<h2>Get Frequency of Character from a string</h2>

Enter Your Text :

 </body>

</html>

<scripts>

var charkey = “”;
function GetTheKey(e) {
charkey = document.getElementById(“txtData”).value.substring(document.getElementById(“txtData”).value.length – 1);
}
function GetFrequency(e) {
debugger
var count = 1;
var value = document.getElementById(“txtData”).value;
if (e.which <= 90 && e.which >= 48) {
var checkCount = 0;
var keyChar = e.key;
if (value.length > 1) {
for (var i = 0; i < value.length – 1; i++) {
if (keyChar == value[i]) {
count++;
}
else
continue;
}

if (count > 1) {
for (var i = 1; i < count; i++) {
if (i != count) {
if (document.getElementById(keyChar + i) != null) {
var eleData = document.getElementById(keyChar + i).innerHTML.replace(/.$/, count);
document.getElementById(keyChar + i).innerHTML = eleData;
checkCount++;
}
}
}
}
if (checkCount == 0) {

var lable = document.createElement(“lable”);
lable.textContent = “The frequency of character ” + keyChar + ” is ” + count;
lable.id = keyChar + count;
document.getElementById(“result”).appendChild(lable);
var br = document.createElement(“br”);
document.getElementById(“result”).appendChild(br);
}
checkCount = 0;
count = 1;
}
else {
var lable = document.createElement(“lable”);
lable.textContent = “The frequency of character ” + keyChar + ” is ” + 1;
lable.id = keyChar + count;
document.getElementById(“result”).appendChild(lable);
var br = document.createElement(“br”);
document.getElementById(“result”).appendChild(br);
count = 1;
}
}
else if (e.which == 8) {
// if (document.getElementById(“txtData”).selectionStart != 0) {

if (document.getElementById(“result”).lastChild != null) {
var ele = document.getElementById(“result”).lastChild;
document.getElementById(“result”).lastChild.remove();
if (document.getElementById(“result”).lastChild != null) {
var iNum = document.getElementById(charkey + count).innerHTML.substring(document.getElementById(charkey + count).innerHTML.length – 1);
if (parseInt(iNum) > 1) {
var eledata = document.getElementById(charkey + count).innerHTML.replace(/.$/, iNum – 1);
document.getElementById(charkey + count).innerHTML = eledata;
document.getElementById(“result”).appendChild(ele);
}
else {
document.getElementById(“result”).lastChild.remove();
}
}
}
}
//}

}

</scripts>

Note:- Do copy and paste above code and remove the last character ‘s’ from script tag.

Any thing not work for you feel free to drop a comment in the comment box; always happy to listen your feedback.

Happy Coading 🙂

#Share2Know @SURYAN_Mode

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s