# Find the character frequency in a given string

Find the character frequency in a given string
0

#1

my logic is failing to print character occurance in a string

``````<center>
<h2>Frequency Calculation</h2>
<table>
<tr>
<td>Enter the string:</td>
<td><textarea cols="10" rows="3 name="string" id="string"></textarea></td>
</tr>
</table>
<button type="button" id="check" onclick="calculateFrequency()">Check Frequency</button>
<div id="result"></div>
</center>
<script >

function calculateFrequency(){

var str = document.getElementById("string").value;
var count = 0;
for(var i=0; i<str.length; i++) {
if(str[i] == str) {
count++;
}
}
//return count;
document.getElementById("result").innerHTML = count;
}

</script>
</body>
</html>
``````

output is attached

#2

What are you checking in the if condition?
If you need to count how many of each character there are, that’s not what does that code.
I am thinking aloud, so to speak, maybe you need some structure to keep track of how many letters are of each one. Then go through the string checking for each letter and sum one if it is the same in the string.
It’s just an idea, to start thinking the problem.
It is useful first try to solve a smaller version “by hand” to get an idea of the problem before beginning to code. Follow each step of the loop to see what’s happening.