Today, sorting out their folders, found that once interviewed Tencent interns were asked to hang the face of the questions, I will be prepared for your reference. It was the handwriting code that was required. Khan, I was in a weak burst!
1, converts the given array to a random array
Array.prototype.shuffle=function () {
var resultarr=[];
var len=this.length;
while (len) {
var _index=parseint (Math.random () *len);
Resultarr.push (This[_index])//Get one character in the array, put it in the new array for
(var i=_index;i<len;i++) {//Backward forward move
this[i]= THIS[I+1];
}
len--;
}
return resultarr;
}
var a=new Array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ');
Alert (A.shuffle ());
2, statistics the number of occurrences of each character in a given string
var a= "Aassdddffffffffggghhhjjjkkkll";
function counts (str) {
var obj={};
for (Var i=0;i<str.length;i++) {
if (!obj[str[i]]) {
obj[str[i]]=1
} else{
obj[str[i]]++
}
}
return obj;
}
Console.log (Counts (a));//object {a:2, s:2, D:3, F:8, G:3 ...}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/
3, to judge the completion of JS code loading
function Dynamicload ()
{
var _doc=document.getelementsbytagname (' head ') [0];
var script=document.createelement (' script ');
Script.setattribute (' type ', ' text/javascript ');
Script.setattribute (' src ', ' jquery.js ');
_doc.appendchild (script);
Non ie IE ,
script.onload=script.onreadystatechange=function () {
//non ie IE , this value is not determined
if (! this.readystate| | this.readystate== ' Loaded ' | | this.readystate== ' complete ') {
alert (' Done ');
Script.onload=script.onreadystatechange=null
}}
}
4, three columns to achieve the layout:
<! DOCTYPE html>
Author: cnblogs Wimber