...

View Full Version : Showing what letter shows up the most



henry_smith56
10-13-2006, 06:06 PM
I'm needing help on how to make this work!
What it needs to do, is to show what the letter that shows up the most and how many times it appears.
Here is the code:
<html>
<head>
<title> Counting Characters </title>
</head>

<body>
<script language = "JavaScript">
//searching characters in a string

var s = prompt("Enter a string");

//count s.charAt(0)
for (i = 0; i < s.length; i++)
{
var cnt1 = 0;
for (i = 0; i < s.length; i++)
if (s.charAt(i) == s.charAt(0))
cnt1++;
}

document.write(cnt1, "<BR>");




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

Please respond as soon as possible!

Philip M
10-13-2006, 07:05 PM
Is this a student's homework assignment?

henry_smith56
10-13-2006, 07:13 PM
yes, i've worked on it last night, and I'm having no luck. Can someone help me?

Philip M
10-14-2006, 08:58 AM
Something like this?

function countMostLetters (which) {
var count = 0;
var finalCount = 0;
var mostChar = 0;
var chars = new Array ('a', 'b', 'c', 'd', .... and so on to z);
var str = which.value.toLowerCase;

for (i = 0; i < chars.length; i++) {
count = 0;
if (str.indexOf(chars[i]) != -1) { // match found
count = count ++;
if count > finalCount {
finalCount = count;
mostChar = char[i];
}
}

alert (mostChar, finalCount);
}

Note that if two letters have equal number of instances (say 3 x 'a's and 3 x 'z's ) only the first is shown.

Mr J
10-14-2006, 05:55 PM
Have a play around with this as well


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">

function countChar(v){
str=document.getElementById("t1").value
tempStr=str

charArr=[]

tempStr=tempStr.replace(/\s/g,"")
strCount=tempStr.length
index=0

while(strCount!=0){
charArr[index]=[]
currentChar=tempStr.substring(0,1)
charArr[index][0]=currentChar

re=new RegExp(currentChar,'gi')

charArr[index][1]=tempStr.match(re).length
tempStr=tempStr.replace(re,'')

index++
strCount=tempStr.length
}

charArr.sort()

document.getElementById("display").innerHTML="The characters in the string <b>"+str+"</b> are as follows<br><br>"

for(var i=0;i<charArr.length;i++){
document.getElementById("display").innerHTML+=charArr[i][0]+" = "+charArr[i][1]+"<br>"
}

}

</script>

</HEAD>
<BODY>

<input type="text" id="t1" value="this is a test">
<input type="button" value="Button" onclick="countChar()">

<BR><BR>

<div id="display"></div>

</BODY>
</HTML>

Philip M
10-15-2006, 09:03 AM
With respect, Henry Smith is a student and I do not think that his tutor would believe for one moment that Mr J's script involving regular expressions and innerHTML was his own work. In any case, it bears little relation to the question asked, which is not "which characters are included in the string" but "which character in the string is most frequent and how often does it occur".

Mr J
10-15-2006, 11:24 AM
With respect, Henry Smith is a student and I do not think that his tutor would believe for one moment that Mr J's script involving regular expressions and innerHTML was his own work.

In hindsight, yes you are correct


In any case, it bears little relation to the question asked, which is not "which characters are included in the string" but "which character in the string is most frequent and how often does it occur".

But the clues are there because if you can get a list of them all that means you only have to extract the one you want



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum