手记 《半年工作经验今日头条和美团面试题面经分享》

1,444 阅读3分钟

最近看到一篇文章《半年工作经验今日头条和美团面试题面经分享》,对于文章里出现的面试题参考各种资料写了一下,有写的不完善的地方希望大家友善讨论~

原文链接:半年工作经验今日头条和美团面试题面经分享 - 掘金

一、css和html

 A元素垂直居中
 A元素距离屏幕左右各边各10px
 A元素里的文字font—size:20px,水平垂直居中
 A元素的高度始终是A元素宽度的50% 

<html>
<head>
	<title>测试css</title>
</head>
<body>
	<div class="father">
		<div class="child">啊啊啊啊</div>
	</div>
</body>
<style>
	.father {
		height: 100%;
		width: 100%;
		display: flex;
		align-items:center;
		text-align: center;
		background:#222;
		padding: 0 10px;
	}

	.child {
		margin: auto;
		width: 100%;  
		padding-top: 25%;  // width和padding百分比的基数 都是父元素的宽度
		padding-bottom: 25%;
		background:#eee;
		font-size: 20px;
		height: 0;
	}
</style>
</html>

二、函数arguments

函数中的arguments是数组吗?怎么转数组?
一、 arguments是类数组, 拥有数组的index和length属性, 不能直接调用数组的方法
二、 类数组转数组的几种方法
1. Array.from(arguments)
2. [...arguments]
3. Array.prototype.slice.call(arguments) 或者 [].prototype.slice.call(arguments)

三、以下打印结果

if([]==false){console.log(1)};
if({}==false){console.log(2)};
if([]){console.log(3)}
if([1]==[1]){console.log(4)}
结果: 输出1 3
==两边
Object类型会先转换为原始类型, 具体是通过valueOf()和toString()方法
1. 左侧: [].valueOf还是[],调用toString()方法, [].toString为''''为String类型,需要转换为Number,为0
   右侧: false为boolean类型,需要转换为number,为0
2. 左侧: 同上,{}.toString为'[object Object]',需要转换为Number,为NaN
   右侧:false为boolean类型,需要转换为number,为0
3. []转换为boolean类型为true
4. 引用地址不同

四、以下输出结果

 async function async1(){
    console.log('async1 start')
    await async2()
    console.log('async1 end')
  }
  async function async2(){
    console.log('async2')
  }
  console.log('script start')
 setTimeout(function(){
    console.log('setTimeout') 
 },0)  
 async1();
 new promise(function(resolve){
    console.log('promise1')
    resolve();
 }).then(function(){
    console.log('promise2')
 })
 console.log('script end')

script start
async1 start
async2
promise1
script end
promise2
async1 end
setTimeout

主要注意 await async2() 相当于 
new Promise(()=>{
	console.log(async2)
}).then((resolve) => {
	resolve()
})

五、手写bind

注意构造函数不能使用箭头函数定义, this指向问题

Function.prototype.binds = function (context) {
    if (typeof this !== "function") {
      throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
    }

    let self = this;
    let beforeArgs = Array.prototype.slice.call(arguments, 1);
    let fNOP = function () {};

    let fbound = function () {
        let lastArgs = Array.prototype.slice.call(arguments);
        self.apply(this instanceof fNOP ? this : context, beforeArgs.concat(lastArgs));
    }

    fNOP.prototype = this.prototype;
    fbound.prototype = new fNOP(); // 使用空函数过渡, 修改新函数prototype时不会对原函数的prototype造成影响

    return fbound;
  }

六、节流函数

// fn是我们需要包装的事件回调, interval是时间间隔的阈值
function throttle(fn, interval) {
  let last = 0
  
  return function () {
      let context = this
      let args = arguments
      let now = +new Date()
      
      if (now - last >= interval) {
          last = now;
          fn.apply(context, args);
      }
    }
}

七、随意给定一个无序的、不重复的数组data,任意抽取n个数,相加和为sum,也可能无解,请写出该函数


/**
 * 获取数组arr内,num个数的全组合
 * 比如 arr = [1,2,3,4], num = 3
 * 返回 [[1,2,3], [1,2,4], [1,3,4], [2,3,4]]
 * @param {*} arr 
 * @param {*} num 
 */
function getCombination(array, number) {
    let result=[];
    (function (group,arr,num){
        if (num == 0) {
            return result.push(group);            
        }
        let length = arr.length
        for (let i = 0; i <= length-num; i++) {
            arguments.callee(group.concat(arr[i]), arr.slice(i + 1), num - 1);
        }
    })([], array, number);
    return result;
}

/**
 * 随意给定一个无序的、不重复的数组data,任意抽取n个数,相加和为sum,也可能无解,请写出该函数
 * @param {*} arr 
 * @param {*} num 
 * @param {*} sum 
 */
function main(arr, num, sum) {
    let result = [];
    let list = getCombination(arr, num);
    for (let item of list) {
        let listSum = item.reduce((pre, current) => {
            return pre + current;
        },0)
        if (listSum == sum) {
            result.push(item);
        }
    }
    return result;
}

const arr = [-1,1,2,3,4,5]
const num = 2;
const sum = 6;
const result = main(arr, num, sum);
console.log(result)


参考文章: