javascript测试题练习代码
1、给你一个字符串,要你找出里面出现次数最多的字母和出现的次数,例如:“abaasdffggghhjjkkgfddsssss”;
代码如下:
var str = "abaasdffggghhjjkkgfddsssss";
var arr = new Array();
var i = 0;
while (str.charAt(0)) {
arr[i] = str.charAt(0) + "=" + (str.split(str.charAt(0)).length - 1);
str = str.split(str.charAt(0)).join("");
i++;
}
alert(arr);
for (var j = 0,temp=0; j < arr.length; j++) {
if (temp <= Number(arr[j].split("=")[1])) {
temp = Number(arr[j].split("=")[1]);
i = j;
}
}
alert(arr[i]);
var f = function(s) {
if (!arguments.length || !s) {
return null;
}
if ("" == s) {
return 0;
}
var l = 0;
for (var i = 0; i < s.length; i++) {
if (s.charCodeAt(i) > 255) {
l += 2;
} else {
l++;
}
}
alert(l);
};
f("你好a")
Array.prototype.strip = function() {
if (this.length < 2) {
return [this[0]] || [];
}
var arr = [];
for (var i = 0; i < this.length; i++) {
arr.push(this.splice(i--, 1));
for (var j = 0; j < this.length; j++) {
if (this[j] == arr[arr.length - 1]) {
this.splice(j--, 1);
}
}
}
return arr;
};
var a = ["abc", "abc", "a", "b", "c", "a", "b", "c"];
alert(a.strip());