Posted in Javascript onOctober 10, 2012
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]);
2、求解字符串的字节长度;
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")
3、去掉数组中重复的元素;
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());
javascript测试题练习代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@