今天整理本身的文件夾時,發現了曾經面試騰訊實習生時的被問掛了的面試題,我將它們整理好了供你們借鑑。當時要求的是手寫代碼。汗啊,我那時弱爆了!javascript
1,將給定數組轉換爲一個隨機數組html
1 Array.prototype.shuffle=function(){ 2 var resultArr=[]; 3 var len=this.length; 4 5 while(len){ 6 var _index=parseInt(Math.random()*len); 7 resultArr.push(this[_index]);//每次取得數組中一個字符,放到新的數組中 8 for(var i=_index;i<len;i++){//後面的向前移動 9 this[i]=this[i+1]; 10 } 11 len--; 12 } 13 return resultArr; 14 } 15 var a=new Array('1','2','3','4','5','6'); 16 alert(a.shuffle());
2,統計給定字符串裏的各個字符出現的次數java
1 var a="aassdddffffffffggghhhjjjkkkll"; 2 function counts(str){ 3 var obj={}; 4 for(var i=0;i<str.length;i++){ 5 if(!obj[str[i]]){ 6 obj[str[i]]=1; 7 }else{ 8 obj[str[i]]++; 9 } 10 11 } 12 return obj; 13 } 14 15 console.log(counts(a));//Object {a: 2, s: 2, d: 3, f: 8, g: 3…}
3,判斷js代碼加載完成jquery
1 function dynamicLoad() 2 { 3 var _doc=document.getElementsByTagName('head')[0]; 4 var script=document.createElement('script'); 5 6 script.setAttribute('type','text/javascript'); 7 script.setAttribute('src','jquery.js'); 8 9 _doc.appendChild(script); 10 //非ie ie, 11 script.onload=script.onreadystatechange=function(){ 12 //非ie ie,該值不肯定 13 if(!this.readyState||this.readyState=='loaded'||this.readyState=='complete'){ 14 alert('done'); 15 script.onload=script.onreadystatechange=null; 16 } 17 } 18 }
4,實現三列布局:面試
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta charset="utf-8"/> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <style> 7 #side { background: #99FF99; height: 300px; width: 120px; float: left; } 8 #main { background: #99FFFF; height: 300px; width: auto; margin-left: 120px; } 9 #main-left{ background:red;float:left; width:200px; height:300px;} 10 #main-right{ margin-left:200px; height:300px; width:auto;} 11 </style> 12 </head> 13 <body> 14 <div id="side">此處爲左邊部分</div> 15 <div id="main"> 16 <div id="main-left">右邊的左列</div> 17 <div id="main-right">右邊的右列</div> 18 19 </div> 20 </body> 21 </html>
開心一刻:數組