View Full Version : Searching for the highest occurance of a character

03-08-2005, 10:00 AM
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);
how should i continue from here

Thanks in advance.

03-08-2005, 10:56 AM
<style type="text/css">
.underlined {text-decoration:underline;}
<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];
<form name="f">
<input type="text" name="txtData" />
<input type="button" value="Underline" onclick="underline(this.form)" />
<div id="data"></div>

This doesn't take into consideration if there are 2 or more vowels with the same number of occurrences.