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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Searching for the highest occurance of a character

    Lets say I have a textbox txtData. I would like to look for the highest number of occurances of a single vowel and underline the vowel in a label. For example "Today's weather is good" , O has the highest occurance rate and thus the script will underline all the Os.

    strData = txtData.value;

    var strVowels = new String("AEIOU")
    var intIndex;
    var intMaxIndex;
    var strCurrentChar;

    intMaxIndex = strData.length-1

    for(intIndex=0;intIndex<=intMaxIndex;intIndex++) {
    strCurrentChar = strData.substr(intIndex,1);
    if(strVowels.indexOf(strCurrentChar)<=0)
    *******
    how should i continue from here
    *******

    Thanks in advance.
    Last edited by siege; 03-08-2005 at 09:02 AM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,951
    Thanks
    0
    Thanked 236 Times in 233 Posts

    Use regexp

    Code:
    <html>
    <head>
    <style type="text/css">
    .underlined {text-decoration:underline;}
    </style>
    <script type="text/javascript">
    function underline(oForm){
      var arr = new Array();
      var strData = oForm.txtData.value;
      var resultsA = strData.match(/a/gi); if (resultsA) arr[arr.length] = [resultsA.length, 'a'];
      var resultsE = strData.match(/e/gi); if (resultsE) arr[arr.length] = [resultsE.length, 'e'];
      var resultsI = strData.match(/i/gi); if (resultsI) arr[arr.length] = [resultsI.length, 'i'];
      var resultsO = strData.match(/o/gi); if (resultsO) arr[arr.length] = [resultsO.length, 'o'];
      var resultsU = strData.match(/u/gi); if (resultsU) arr[arr.length] = [resultsU.length, 'u'];
    
      if (arr.length > 0) {
        arr.sort(compareFunc); //sort in desc order    
        var ch = arr[0][1];
        var re = new RegExp(ch, "gi");
        strData = strData.replace(re, '<span class="underlined">'+ch+'</span>');
      }
      document.getElementById("data").innerHTML = strData;
    }
    
    function compareFunc(a, b){
       return b[0] - a[0];
    }
    </script>
    </head>
    <body>
    <form name="f">
    <input type="text" name="txtData" />
    <input type="button" value="Underline" onclick="underline(this.form)" />
    <div id="data"></div>
    </form>
    </body>
    </html>
    This doesn't take into consideration if there are 2 or more vowels with the same number of occurrences.


  •  

    Posting Permissions

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