Web Analytics Made Easy -
StatCounter Showing what letter shows up the most - CodingForum

Announcement

Collapse
No announcement yet.

Showing what letter shows up the most

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Is this a student's homework assignment?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      yes, i've worked on it last night, and I'm having no luck. Can someone help me?

      Comment


      • #4
        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.

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          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.

          Comment


          • #6
            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".

            All the code given in this post has been tested and is intended to address the question asked.
            Unless stated otherwise it is not just a demonstration.

            Comment


            • #7
              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.

              Comment

              Working...
              X