Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Showing what letter shows up the most

    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!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Is this a student's homework assignment?

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, i've worked on it last night, and I'm having no luck. Can someone help me?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Have a play around with this as well

    PHP Code:
    <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> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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".

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •