js的6道基础题(笔试常考题)

转载:http://www.bubuko.com/infodetail-20477.html

题目一:找出数字数组中最大的元素

var arr=[0,1,2,3,4,5,6,7,8,9];
console.log(Math.max.apply(null,arr))

题目二:转化一个数字数组为function数组(每个function都弹出相应的数字)

for循环闭包的问题

var arr=[0,1,2,3,4,5,6,7,8,9],arrFunc = [];
for(var i = 0, l = arr.length; i < l; i++){
  arrFunc.push((function(i) {
    return function() {
      console.log(arr[i]);
    }
  })(i))
}
题目三:给object数组进行排序(排序条件是每个元素对象的属性个数)

Object.prototype.myLength = function(){
  var length = 0;
  for(var i in this){
    length ++;
  }
  return length;
}
var objArr = [
  {a:1, b:2, c:5, d:7, e:8, g:0, h:12, i:5, v:9, w:9, x:9, y:9, z: 15},
  {a:2, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, l:9, m:9, n:0, o:1, p:9, x:9, y:9, z:9 }, 
  {a:3, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, l:9, m:9, n:0, o:1, p:9, q:0 },
  {a:4, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, w:9, x:9, y:9, z:9 },
  {a:5, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, v:9, w:9, x:9, y:9, z:9 },
  {a:6, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, l:9, m:9, n:0, o:1, p:9, q:0, r:8, s:9, t:9, z:9 },
  {a:7, b:2, c:5, d:7, e:8, x:9, y:9, z:9 }
];
// arr before sort
var numArr1 = []
for(var i = 0, l = objArr.length; i < l; i++ ){
  numArr1.push( objArr[i].myLength() )
}
console.log(numArr1.join(" ")) //result
// arr after sort
objArr.sort(function(a,b){
  // stable sort
  // return (a.myLength() > b.myLength()) === true? 1:-1;
  // unstable sort
  return (a.myLength() >= b.myLength()) === true? 1:-1;
  // return a.myLength() - b.myLength();
})
var numArr2 = []
for(var i = 0, l = objArr.length; i < l; i++ ){
  // console.log(i,l,objArr[i].myLength());
  numArr2.push( objArr[i].myLength() )
}
console.log(numArr2.join(" ")) //result

题目四:利用JavaScript打印出Fibonacci数(不使用全局变量)

var fibonacci = (function(){
  var s = [];
  var fun = function(x) {
    if(s[x]){
      return s[x];
    }
    if(x < 0) {
      throw "Can‘t be negative";
      return ;
    }
    else if(x === 0 || x === 1) {
      s[x] = s[x] || x;
      return s[x];
    }
    else{
      s[x] = ( fun(x - 1) + fun(x - 2) );
      return s[x];
    }
  };
  fun.print = function() {
    console.log(s.join(" "));
  }
  fun.printLast = function() {
    // console.log(s.length);
    return(s[s.length-1]);
  }
  window.s = s;
  return fun;

})()
console.time(200);
console.log(fibonacci(200));
console.log(fibonacci.printLast());
console.log(fibonacci.print());
console.timeEnd(200);
var fibonacci2 = function(x){
  if(x < 0) {
    throw "Can‘t be negative";
    return ;
  }
  if(x === 0 || x === 1) {
    return x;
  }
  var num = ( fibonacci2(x - 1) + fibonacci2(x - 2) )
  return num;
}
console.time(32);
console.log(fibonacci2(32));
console.timeEnd(32);

题目五:实现如下语法的功能:var a = (5).plus(3).minus(6);

Number.prototype.plus = function(x) {
  var num = this.valueOf() + x;
  return Number(num);
}
Number.prototype.minus = function(x) {
  var num = this.valueOf() - x;
  return Number(num);
}

var a = (5).plus(3).minus(6);
console.log(a);
alert(a);
题目六:实现如下语法的功能:var a = add(2)(3)(4);
function add(x) {
  var mid;
  mid = x || 0;
  function addObj(x) {
    x = x || 0;
    mid = mid + x;
    return addObj;
  }
  addObj.valueOf = function() {
    return mid;
  }
  addObj.toString = function() {
    return mid;
  }
  return addObj;
}
//call the obj.valueOf function
console.log(add(2));
console.log(add(2)(3));
console.log(add(2)(3)(4));
console.log(add(2)(3)(4)(5));

//call the obj.toString function
alert(add(2));
alert(add(2)(3));
alert(add(2)(3)(4));
alert(add(2)(3)(4)(5));


你可能感兴趣的:(JavaScript,笔试题)