Home >Web Front-end >JS Tutorial >JavaScript test question practice code_javascript skills

JavaScript test question practice code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:49:22997browse

1. You are given a string and asked to find the letter that appears most often and the number of times it appears, for example: "abaasdffggghhjkkgfddsssss";

Copy code The code is as follows:

var str = "abaasdffggghhjkkgfddsssss";
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. Find the byte length of the string ;
Copy code The code is as follows:

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("Hello a")

3. Remove Repeated elements in the array;
Copy code The code is as follows:

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());
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn