各大公司面试题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>各大公司面试题</title>
<script>
//----------------------------------------------------------------------------------------------------------------------------------
//用js实现任意 一个排列算法
Array.prototype.sort2 = function (bLift){
var res = [],
_this = this;
while(this.length)
{
(function (){
var iMin = Math.max.apply({}, _this) + 1;
var indexof = 0;
for(var i=_this.length;i--;){
if(_this[i]<iMin){
iMin = _this[i];
indexof = i;
}
}
//alert(iMin)
_this.splice(indexof, 1);
res.push(iMin);
})();
}
for(var i=res.length;i--;) this[this.length] = bLift ? res.pop() : res.shift();
return this;
};
var arr = [6,13,47,25,1];
//alert(arr.sort2(true)); //true or false --> 升序 or 降序
//----------------------------------------------------------------------------------------------------------------------------------
//把数组var arr=[34,2,1,6,2,6,90,3]; 里重复的项去掉
Array.prototype.delRepeat = function (){
for(var i=0;i<this.length;i++){
for(var j=i+1;j<this.length;j++){
if(this[i] == this[j]){
this.splice(j,1);
j--;
}
}
}
return this;
};
var arr2 = [5,1,2,2,3,1,2,5];
//alert(arr2.delRepeat())
//----------------------------------------------------------------------------------------------------------------------------------
/*
写一段程序,在下面这个字符中,查找字母“c”在文本中出现的次数,分别在第几位?
var str='I love JavaScript and css';
*/
String.prototype.indexofStr = function(str, bIgnore){
var arr = [];
var obj = {times:str + ' \u5b57\u7b26\u5171\u51fa\u73b0 ', index:[]};
for(var i=this.length;i--;) bIgnore ? str.toUpperCase() == this.charAt(i).toUpperCase() && arr.push(i) : str == this.charAt(i) && arr.push(i);
obj.times += arr.length + ' \u6b21';
for(var i=arr.length;i--;) obj.index[obj.index.length] = arr[i];
obj.index = str + ' \u5b57\u7b26\u4f4d\u7f6e\u5206\u522b\u5728\uff1a' + obj.index + ' \u4f4d';
return obj;
};
var str='I love JavaScript and css';
//true or false --> 忽略大小写
//times --> 出现次数 || index --> 出现位置
//alert(str.indexofStr('i', true).times);
//alert(str.indexofStr('c').index);
//----------------------------------------------------------------------------------------------------------------------------------
/*
写一个append方法,可以直接添加节点?(添加前置节点与后入式节点)
用法 append(oUl,'<li>111</li><li>222</li>');
*/
function append(parent, childs, bFalse){
var oP = document.getElementById(parent);
var oBox = document.createElement('div');
oBox.innerHTML = childs;
var arr = [];
for(var i=0;i<oBox.children.length;i++) arr.push(oBox.children[i]);
for(var i=0;i<arr.length;i++) bFalse ? oP.insertBefore(arr[i], oP.children[0]) : oP.appendChild(arr[i]);
};
//----------------------------------------------------------------------------------------------------------------------------------
/*
编写一个JavaScript函数 parseQueryString,它的用途是把URL参数解析为一个对象:
var obj = parseQueryString(url);
用法 www.miaov.com?key1=123&key2=456; alert(obj.key1) //123
*/
String.prototype.parseQueryString = function (key){
var arr = this.slice(this.indexOf('?')+1).split('&');
var obj = {},
arr2 = [];
for(var i=0;i<arr.length;i++){
arr2[i] = arr[i].split('=');
obj[arr2[i][0]] = arr2[i][1];
}
return obj[key];
};
var url = 'www.miaov.com?aaa=123&bbb=456';
//alert(url.parseQueryString('bbb'));
//----------------------------------------------------------------------------------------------------------------------------------
/*
写一个获取元素相对于屏幕距离的方法:getPos()
用法 :var R = getPos(obj).right; var B = getPos(obj).bottom;
*/
function getPos(element){
var oEle = document.getElementById(element);
var l = 0,
t = 0,
r = 0,
b = 0,
w = oEle.offsetWidth,
h = oEle.offsetHeight;
while(oEle){
l += oEle.offsetLeft;
t += oEle.offsetTop;
oEle = oEle.offsetParent;
}
r = ((document.documentElement.clientWidth || document.body.clientWidth) - (l + w));
b = ((document.documentElement.clientHeight || document.body.clientHeight) - (t + h));
return {left:'\u5de6\u8fb9\u8ddd\u53ef\u89c6\u533a '+l,
top:'\u4e0a\u8fb9\u8ddd\u53ef\u89c6\u533a '+t,
right:'\u53f3\u8fb9\u8ddd\u53ef\u89c6\u533a '+r,
bottom:'\u4e0b\u8fb9\u8ddd\u53ef\u89c6\u533a '+b};
};
//----------------------------------------------------------------------------------------------------------------------------------
//封装一个事件绑定函数bind()
function bind(obj, sEv, fn){
obj.attachEvent ? obj.attachEvent('on'+sEv, fn) : obj.addEventListener(sEv, fn, false);
};
//----------------------------------------------------------------------------------------------------------------------------------
//阶乘(阶乘指从1乘以2乘以3乘以4一直乘到所要求的数)例如:algorithm(7):返回1*2*3*4*5*6*7的结果
function algorithm(n){
/*
var res = 1;
for(var i=1;i<=n;i++) res *= i;
return res
*/
if(n <= 1) return 1;
else return n*algorithm(--n); //递归计算
};
//alert(algorithm(6));
//----------------------------------------------------------------------------------------------------------------------------------
//写出能输出明天日期的程序(添加输出N天后的功能)
function tomorrow(vDate, step){
var re = /\d+/g;
var step = step || 1;
var aDate = function (){
switch(typeof vDate){
case 'string':
return vDate;
break;
case 'object':
return vDate.getFullYear()+ '-' +(vDate.getMonth()+1)+ '-' +vDate.getDate();
break;
}
}().match(re);
var upDate = parseInt(aDate[2])+step;
var sDate = new Date(aDate[0], parseInt(aDate[1])-1, upDate).toLocaleDateString();
return aDate[0]+'\u5e74'+aDate[1]+'\u6708'+aDate[2]+'\u65e5 '+step+' \u5929\u540e\u662f '+sDate;
};
//指定日期支持多数字符串格式,但年月日之间需要用非数字字符来分隔
//alert(tomorrow('2012~12~31', 10))//返回指定日期多少天后的信息
//alert(tomorrow(new Date()))
//----------------------------------------------------------------------------------------------------------------------------------
/*
随意一个字符串,比如:'Miaov is a IT traning orgnization'
找出其中重复最多的字符,输出这个字符和它出现的次数(忽略大小写)
*/
String.prototype.find = function (bIgnore){
var str = this.replace(/\s/g, '');
var iMax = 0;
var char = '';
for(var i=str.length;i--;){
var re = new RegExp(str.charAt(0), 'g' + (bIgnore ? 'i' : ""));
var tmp = str.match(re);
iMax < tmp.length && (iMax = tmp.length, char = tmp[0]);
str = str.replace(re, '');
}
return {times:'\u6700\u591a\u7684\u5b57\u7b26\u51fa\u73b0\u4e86 '+iMax+' \u6b21',
char:'\u8fd9\u4e2a\u5b57\u7b26\u662f '+char};
};
var str = 'AAabaababbb';
//true or false --> 忽略大小写
//times --> 出现次数 || char --> 出现字符
//alert(str.find(true).times);
//alert(str.find().char);
//----------------------------------------------------------------------------------------------------------------------------------
//为数组写一个indexOf()方法
Array.prototype.indexOf = function (num){
for(var i=this.length;i--;) if(num === this[i]) return i;
return -1;
};
var arr = [1,2,3,4,5];
//alert(arr.indexOf(6));
//----------------------------------------------------------------------------------------------------------------------------------
//写一个方法,把字符串反转,例如:reversal(str)
String.prototype.reverse = function (delimiter){
var re = delimiter || '';
return this.split(re).reverse().join(re);
};
var str = 'I Love You';
//alert(str.reverse(' ')) //方法里不传切割符默认是 ''
//----------------------------------------------------------------------------------------------------------------------------------
/*
写一个方法region(),获取数字的区间
示例1:alert(region(3,6));
弹出结果:4,5
*/
function region(a,b){
var arr = [];
if(a < b) for(var i=a+1;i<b;i++) arr.push(i);
else if(a > b) for(var i=a-1;i>b;i--) arr.push(i);
else arr.push('参数相等,无法计算');
return arr;
};
//alert(region(14,-4));
//----------------------------------------------------------------------------------------------------------------------------------
/*
写一个方法,为元素添加class,
使用方式:addClass(obj,'b');
使用前:<div class="a"></div>
使用后:<div class="a b"></div>
如果该元素内已有一个class名为“b”,就不能重复添加
*/
function addClass(obj, sClass){
var oEle = document.getElementById(obj);
var str = oEle.className || '';
re = new RegExp(sClass);
!re.test(str) && (oEle.className += str.length ? ' '+sClass : sClass);
};
//----------------------------------------------------------------------------------------------------------------------------------
//编写一个js函数toRGB,它的作用是转换CSS中常用的颜色编码(添加随机颜色值)
function css(obj, attr){
var oEle = typeof obj === 'string' ? document.getElementById(obj) : obj;
return oEle.currentStyle ? oEle.currentStyle[attr] : getComputedStyle(oEle, false)[attr];
};
function Rnd(min, max)
{
return Math.floor(Math.random()*(max-min+1)+min);
};
function toRGB(sValue){
var aRGB = [];
var sValue = function ()
{
switch(sValue.charAt(0)){
case '#':
return sValue.substring(1);
break;
default:
return sValue;
break;
}
}();
while(sValue.length){
aRGB.push(parseInt(sValue.substring(0,2),16));
sValue = sValue.substring(2);
}
return 'RGB('+aRGB+')';
}
function getColor(bRGB){
var sValue = Rnd(0, 0xFFFFFF).toString(16);
sValue.length < 6 && (sValue = '0' + sValue);
return bRGB ? toRGB(sValue) : '#'+sValue;
};
//alert(getColor(true));//true or false转换成rgb(xxx,xxx,xxx) or #xxxxxx格式
//alert(toRGB('ffffff'));
//----------------------------------------------------------------------------------------------------------------------------------
//打印json,模仿console.log
var data = {
result:true,
data:[
{assetId:1, 'name':'电影'},
{assetId:2, 'name':'电视'},
{assetId:3, 'name':'人文'},
{assetId:4, 'name':'体育'}
]
};
function echoJson(obj)
{
try{
return JSON.stringify(obj);
}catch(e){
alert(e+' \u8bf7\u4f7f\u7528IE8\u53ca\u4ee5\u4e0a\u9ad8\u7ea7\u6d4f\u89c8\u5668\u67e5\u770b');
}
};
function echo(obj)
{
return obj;
};
//----------------------------------------------------------------------------------------------------------------------------------
//输入字符检测功能,英文/半角为半个字符,汉字/全角为一个字符
String.prototype.len = function (){
return Math.floor(this.replace(/[^\x00-\xff]/g, 'mm').length/2);
};
var advStr = ',?)a!啊1m做2';
//alert(advStr.len());//半角字符占0.5个字符长度
var EnStr = '!2a,?';
//alert(EnStr.len())//半角字符占0.5个字符长度
var CHStr = '》饿他、';
//alert(CHStr.len())//全角字符占1个字符长度
//----------------------------------------------------------------------------------------------------------------------------------
window.onload = function (){
//添加标签元素
//append('ul1','<li><a href="###"><span>555</span></a></li><li>666</li>',true); //父级必须指定Id || true or false --> 前插 or 后插
//获取元素相对屏幕距离
//alert(getPos('div2').left); //元素必须指定id
//alert(getPos('div2').top); //元素必须指定id
//alert(getPos('div2').right); //元素必须指定id
//alert(getPos('div2').bottom); //元素必须指定id
//添加class
//addClass('div2', 'a'); //元素必须指定id
//addClass('div2', 'b'); //元素必须指定id
//addClass('div2', 'a'); //元素必须指定id
var oTxt = document.getElementById('txt1');
//oTxt.innerHTML = echoJson(data);//打印json
//oTxt.innerHTML = echo(data.data[0].name);//打印json
};
</script>
</head>
<body>
<ul id="ul1">
<li>111</li>
<li>222</li>
<li>333</li>
</ul>
<div id="div1" style="width:100px; height:100px; background:#0ede62; position:relative;">
<div id="div2" style="position:absolute; top:0; left:0; width:50px; height:50px; background:red"></div>
</div>
<textarea id="txt1" name="name" rows="10" cols="40"></textarea>
</body>
</html>