常见的前端测试题

205 阅读2分钟

1.输入年月日,输出是今年的第几天。

 function isRun(year){
            return year%4==0&&year%100!=0||year%400==0;
        }

        function day(year,month,day){
            const days=[31,28,31,30,31,30,31,31,30,31,30,31];

            if(month==1){
                return day
            }else{
                for(let i=0;i<month-1;i++){
                    day+=days[i];
                }
                if(isRun(year)&&month>2){
                    day++;
             
              }
              return day;
            }
        }

        console.log(day(2021,3,5));
       

//总结:判断是否闰年,可以被4整除的一般是闰年,被100整除的年份,要能被400整除

// return year%4==0&&year%100!=0||year%400==0;

//除了7 8月连续31天,其他都是间隔的天数

2.查找多数元素(占数组一半以上的元素)

    let arr=[4,8,6,8,8,6,8,8];

    function isMost(array){

        const length=array.length;
      
        let nums={};
        for(let i=0;i<length;i++){
            if(nums[array[i]]==undefined){
                nums[array[i]]=1;
            }else{
                nums[array[i]]+=1;
            }
        }
        for(let a in nums){
            console.table(nums[a],a);
            if(nums[a]>length/2)return a;
        }
        
        return "没有数超过一半"
    }

    console.log(isMost(arr));

思路:

使用对象的key存储当前的值信息,遍历看当前是否为undefined

不是undefined说明再次出现了这个数值,在这个对象的数值里面+1

 结束后的对象
 nums={
     4:1,
     
     6:2,   
     
     8:5
 }

for in遍历对象里面的value是否有超过一半的数字

3.判断输出的值

let obj={
            name:"bytedance",
            getname(){
                return this.name;
            }
        }

        let fb=obj.getname;
        fb();
        console.log(this.name,this);

//window 里面有name 属性,值为" ", (name:" ") 结果为“ ”

4.判断分别输出什么

 	var arr=[];
        arr["a"]=1;
        console.log(arr.length);//0
        arr["4"]=2;
        console.log(arr.length);//5
         arr.length=0;
        console.log(arr);//[a:1]

5.每隔一秒按顺序输出i值

  for (var i=0;i<5;i++){

            (function(i){
                setTimeout(() => {
                    console.log(i);
                }, 1000);
            })(i)
        }

如果是var,就需要用函数传达参数,注意函数里的i和外部i的区别

方法2:es6块级作用域let

  for (let i=0;i<5;i++){

            (function(){
                setTimeout(() => {
                    console.log(i);
                }, 1000*i);
            })()
        }

6.判断

  var f=function g(){
            return 23;
        }
        console.log(typeof g());

报错,g() is no defined

7.、给定一个排序好的数组,判断其中是否存在两个数之和等于指定的值,时间复杂度最好能达到O(n)。

var twoSum=function(arr,target){
    var last={};
    for(let i=0;i<arr.length;i++){
        if(last[arr[i]]=="gooen"){
            console.log(last);
            return true;
        };
        last[target-arr[i]]="gooen"; //随便取个名字
    }
    console.log(last);
    return false;
}

let array=[1,6,8,9,11,13]
console.log(twoSum(array,9));

8.实现map对构造函数,只能通过map提供的接口访问

function MyMap(){ 
    this.map=new Object();
    this.length=0;

    this.size=function(){           //size方法
        return this.length;
    };

    this.set=function(key,value){   //set方法
        if(!this.map[key]){
            ++this.length;
        }
        this.map[key]=value;
    }

   this.has=function(key){       //has方法
     return this.map[key]?true:false;
   }

   this.get=function(key){          //get方法
    return this.map[key]?this.map[key]:undefined;
   }

   this.clear=function(){		//clear方法
       this.length=0;
       this.map=new Object();
   }

   this.delete=function(key){    //delete方法
    if(this.map[key]){
        --this.length;
        delete this.map[key];
        return true;
    }
    return false;
   }

}