日期:2014-05-16  浏览次数:20400 次

急求一个js的试题,有答案就结分
3. 给你一个字符串String=“adadfdfseffserfefsefseetsdg”;要你找出里面出现次数最多的字母和出现的次数,使用javascript来实现!

4. 请给下面javascipt函数每一行加上注释?并说明一下这个函数的作用?
function       doSubmit(value){      
    document.Main.action.value       =       value;      
    document.Main.submit();      
    }      


------解决方案--------------------
<script type= "text/javascript ">
function reStringInfo(s) {
var a = new Array, b = new Object, i, n;
for (i = 0 ; i < s.length ; i ++) {
n = s.charAt(i);
if (b[n]) b[n].num ++;
else a[a.length] = b[n] = {
name : n, num : 1
};
}
return a.sort(function (l, r) {
var a = l.num, b = r.num;
return a > b ? -1 : 1;
})[0];
}

var string = "adadfdfseffserfefsefseetsdg ";
reStringInfo(string);
</script>
------解决方案--------------------
<script language= "javascript ">
var str = "adadfdfseffserfefsefseetsdg ";
var num = 0;
var char = " ";
while(str.length> 0)
{
var tempstr = str.substr(0,1);
var re = new RegExp(tempstr, "g ");
var tempint = str.match(re).length;
if(tempint> num)
{
num = tempint;
char = tempstr;
}
str = str.replace(re, " ");
}
alert( "出现最多的是\ " " + char + "\ ",出现 " + num + "次 ");
</script>

第3题