头条面试

  1. 斐波那契数列 [1,1,1,3,5,9,17]判断第n项的值(递归)
    https://www.cnblogs.com/superlizhao/p/11603158.html
 //数学公式总结

function fib(n){
if(n>0&&n<4){
return 1}
return 2**(n-3)+1
   //最差的方法当属递归,因为每个值都需要堆栈储存空间
    function loop(n){
      let res=0;
     if(n<4){ 
         return 1
   }else{
    res=loop(n-1)+loop(n-2)+loop(n-3)
   }
     return res
   }
   loop(4)//3
``` //1、1、2、3、5、8、13、21第n项合
//数学方法
//递归方法
function fibonacci(n) {
    if (n == 1 || n == 2) {
        return 1
    };
    return fibonacci(n - 2) + fibonacci(n - 1);
}
//优化递归方法
function fibonacci(n) {
    function fib(n, v1, v2) {
        if (n == 1)
            return v1;
        if (n == 2)
            return v2;
        else
            return fib(n - 1, v2, v1 + v2)
    }
    return fib(n, 1, 1)
}
fibonacci(30)
//方法三:改进递归-利用闭包特性把运算结果存储在数组里,避免重复计算
var fibonacci = function () {
    let memo = [0, 1];
    let fib = function (n) {
        if (memo[n] == undefined) {
            memo[n] = fib(n - 2) + fib(n - 1)
        }
        return memo[n]
    }
    return fib;
}()
fibonacci(30)
//方法三1:改进递归-摘出存储计算结果的功能函数
var memoizer = function (func) {
    let memo = [];
    return function (n) {
        if (memo[n] == undefined) {
            memo[n] = func(n)
        }
        return memo[n]
    }
};
var fibonacci=memoizer(function(n){
    if (n == 1 || n == 2) {
        return 1
    };
    return fibonacci(n - 2) + fibonacci(n - 1);
})
fibonacci(30)
//循环
function fibonacci(n) {
    var n1 = 1, n2 = 1, sum;
    for (let i = 2; i < n; i++) {
        sum = n1 + n2
        n1 = n2
        n2 = sum
    }
    return sum
}
fibonacci(30)
//循环
var fibonacci = function (n) {
    let n1 = 1; n2 = 1;
    for (let i = 2; i < n; i++) {
        [n1, n2] = [n2, n1 + n2]
    }
    return n2
}
fibonacci(30)

  1. 状态码

    • 100\color{red}{接受的请求正在处理}
    • 101\color{red}{需要切换协议}
    • 200\color{red}{表示从客户端发送给服务器的请求被正常处理并返回}
    • 204\color{red}{ No Content表示客户端发送给客户端的请求得到了成功处理,但在返回的响应报文中不含实体的主体部分(没有资源可以返回)}
    • 206\color{red}{Patial Content服务器成功执行了部分的GET请求,响应报文中包含由Content-Range指定范围的实体内容}
    • 301\color{red}{永久重定向,资源转移到其他URL}
    • 302\color{red}{临时重定向,资源转移到其他URL}
    • 303\color{red}{临时重定向,提示客户端应当采用GET方式获取资源}
    • 304\color{red}{资源取得缓存值,If - Modified-Since 和If-None-Natch 这两项,服务器就是通过检查这两项来判断是否是已经做了缓存}
    • 307\color{red}{临时重定向,与303有着相同的含义}
    • 400\color{red}{Bad Request表示请求报文中存在语法错误}
    • 401\color{red}{Unauthorized:未经许可,需要通过HTTP认证}
    • 403\color{red}{Forbidden服务器拒绝该次访问(访问权限出现问题)}
    • 404\color{red}{Not Found:表示服务器上无法找到请求的资源,除此之外,也可以在服务器拒绝请求但不想给拒绝原因时使用}
    • 500\color{red}{Not Found:Inter Server Error:表示服务器在执行请求时发生了错误,也有可能是web应用存在的bug或某些临时的错误时}
    • 503\color{red}{Server Unavailable:表示服务器暂时处于超负载或正在进行停机维护,无法处理请求}
  2. 浏览器缓存设置

  3. 判断值类型

    var a = "string.",b = 123,c= [1,2,3],d = new Date(),e=null;f = function(){this.name="22";};
    // typeof
    typeof a ------------> string
    typeof b------------> number
    typeof c------------> object
    typeof d------------> object
    typeof e------------> object
    typeof f------------> function
    // instanceof
    c instanceof Array---------------> true
    d instanceof Date---------------> true
    f instanceof Function------------> true
    f instanceof function------------> false
    //constructor在继承类下容易出错
    c.constructor === Array----------> true
    d.constructor === Date-----------> true
    e.constructor === Object-------> true
       function A(){};
       function B(){};
       A.prototype = new B(); //A继承自B
       var aObj = new A();
       aobj.constructor === B -----------> true;
       aobj.constructor === A -----------> false; 
    //prototype
    Object.prototype.toString.call(a) ==='[object String]'-------> true;
    Object.prototype.toString.call(b) ==='[object Number]'-------> true;
    Object.prototype.toString.call(c) ==='[object Array]' -------> true;
    Object.prototype.toString.call(d) ==='[object Date]' -------> true;
    Object.prototype.toString.call(e) ==='[object Null]' -------> true;
    Object.prototype.toString.call(f) ==='[object Function]' -------> true;
    //Jquery
    jQuery.type( undefined ) === "undefined"
    jQuery.type() === "undefined"
    jQuery.type( window.notDefined ) === "undefined"
    jQuery.type( null ) === "null"
    jQuery.type( true ) === "boolean"
    jQuery.type( 3 ) === "number"
    jQuery.type( "test" ) === "string"
    jQuery.type( function(){} ) === "function"
    jQuery.type( [] ) === "array"
    jQuery.type( new Date() ) === "date"
    jQuery.type( new Error() ) === "error" // as of jQuery 1.9
    jQuery.type( /test/ ) === "regexp"
    
  4. 手动实现js的call, apply, bind, instanceof方法

     /**
       * 实现一个caLl方法
     */
    
     Function.prototype.mycall = function(context) {
     //判断是否传入指定的对象
     context = context || window;
    //拿到传入的参数
    let args = [...arguments].slice(1);
    //把调用的函数添加到对象中
    context.fn = this;
    //执行函数
    let result = context.fn(...args);
    //执行函数后,从对象中删除掉函数
    delete context.fn;
    return result;
    }
    
    function Test() {
    console.log(this.name);
    console.log(this.sex);
    }
    
     let obj = {
     name: '李四',
     sex: '男'
     }
    
    Test.mycall(obj)//李四 男
     
    
    
    /**
      * 实现apply方法
      * 大体实现方法和apply相同
    */
    
    Function.prototype.myapply = function(context) {
    context = context || window;
    context.fn = this;
    let result;
    if (arguments[1]) {
    result = context.fn(...arguments[1]);
    } else {
    result = context.fn();
    }
    delete context.fn;
     return result;
     }
    
      function fn(name, sex) {
      this.name = name;
      this.sex  = sex;
      console.log(this.name);
      console.log(this.sex);
       }
    
     let obj = {};
     fn.myapply(obj,['张三','女']);//张三  女
    
    
     /**
       * 实现一个bind函数
     */
    Function.prototype.mybind = 
    function(context) {
    if (typeof this !== 'function') {
     return new Error("不是一个函数");
     }
    let _this = this; //保存当前函数
    let args = [...arguments].slice(1);
    return function F(...newArgs) {
    //bind返回的是个函数,所以可以实例化一个对象返回
    if (this instanceof F) {
      return new _this(...args,...newArgs);
    } else {
     return _this.apply(context,args.concat(newArgs));
    }
     }
    }
    
    function parent(sex) {
    console.log(sex);
    console.log(this.name);
    
    }
    let Son = {
    name: 'zhangsan'
    }
    let son = parent.mybind(Son,'男');
    son();//男 张三
    
    
    
    
       /**
         * 实现一个instanceof的判断方法
         * instanceof方法的判断是通过隐式原型属性__proto__判断的
        * 只要在这条原型链上的对象都为true
      */
    
        function myInstanceoF(left, right) {
        //保存左边构造函数的原型对象
         let prototype = right.prototype;
         left = left.__proto__;
         while (true) {
          if (left == null) {
         return false;
         } else if (left == prototype) {
         return true;
         }
           left = left.__proto__;
        }
         }
        function Hello(name) {
         this.name = name;
         }
       let test = new Hello('张三');
       let arr = new Array('33');
       console.log(myInstanceoF(arr, Array));
     console.log(myInstanceoF(test, Hello));
    console.log(myInstanceoF(arr, Hello));
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。