欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-11 08:30:50
js的6道基础题(笔试常考题)。 题目一:找出数字数组中最大的元素 var arr=[0,1,2,3,4,5,6,7,8,9]; console.log(math.m...

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

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

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));