javascript测试题练习

1、给你一个字符串,要你找出里面出现次数最多的字母和出现的次数,例如:“abaasdffggghhjjkkgfddsssss”;

 1 var str = "abaasdffggghhjjkkgfddsssss";

 2     var arr = new Array();

 3     var i = 0;

 4     while (str.charAt(0)) {

 5         arr[i] = str.charAt(0) + "=" + (str.split(str.charAt(0)).length - 1);

 6         str = str.split(str.charAt(0)).join("");

 7         i++;

 8     }

 9     alert(arr);

10     for (var j = 0,temp=0; j < arr.length; j++) {

11         if (temp <= Number(arr[j].split("=")[1])) {

12             temp = Number(arr[j].split("=")[1]);

13             i = j;

14         }

15     }

16     alert(arr[i]);

 2、求解字符串的字节长度;

 1 var f = function(s) {

 2         if (!arguments.length || !s) {

 3             return null;

 4         }

 5         if ("" == s) {

 6             return 0;

 7         }

 8         var l = 0;

 9         for (var i = 0; i < s.length; i++) {

10             if (s.charCodeAt(i) > 255) {

11                 l += 2;

12             } else {

13                 l++;

14             }

15         }

16         alert(l);

17     };

18     f("你好a")

3、去掉数组中重复的元素;

 1  Array.prototype.strip = function() {

 2         if (this.length < 2) {

 3             return [this[0]] || [];

 4         }

 5         var arr = [];

 6         for (var i = 0; i < this.length; i++) {

 7             arr.push(this.splice(i--, 1));

 8             for (var j = 0; j < this.length; j++) {

 9                 if (this[j] == arr[arr.length - 1]) {

10                     this.splice(j--, 1);

11                 }

12             }

13         }

14         return arr;

15     };

16     var a = ["abc", "abc", "a", "b", "c", "a", "b", "c"];

17     alert(a.strip());

 

 

你可能感兴趣的:(JavaScript)