前端面试高频笔试题,前端面试要做到提前准备提前练习,刷一定的面试题笔试题量,面试才能事半功倍一路畅通。

1. 实现快速排序

var arr = [9, 4, 3, 1, 6, 3, 8, 7]
/**
 * 快速排序
 * @param {array} arr 需要排序的数组
 * @returns {array}
 */
function quickSort (arr) {
    if (arr.length <= 1) return arr
    var arr1 = [], arr2 = []
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] < arr[0]) {
            arr1.push(arr[i])
        } else {
            arr2.push(arr[i])
        }
    }
    arr1 = quickSort(arr1)
    arr2 = quickSort(arr2)
    arr1.push(arr[0])
    return arr1.concat(arr2)
}
console.log(quickSort(arr))    // [1, 3, 3, 4, 6, 7, 8, 9]

步骤分析:

2. 实现 add(1)(2,3)(4,5,6) 的调用方式

function add(...args) {
    var f = add.bind(null, ...args) 
    f.sum = () => {
        return args.reduce((a, b) => a + b, 0)
    }
    return f
}
console.log(add(1)(2,3)(4,5,6).sum())    // 21

步骤分析:

3. 深拷贝


function clone (obj) {
    if (obj === null || typeof obj !== 'object') return obj
    // 处理数组和对象
    if (obj instanceof Array || obj instanceof Object) {
        var copy = obj instanceof Array ? [] : {}
        for (let item in obj) {
            copy[item] = clone(obj[item])
        }
        return copy
    }
}
var data = {
    a: 1,
    b: [ 2, 3, 4 ],
    c: {
        d: 5
        e: [ 6 ]
    }
}
console.log(clone(data))

步骤分析:

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')
})
async1()
new Promise(function (resolve) {
    console.log('promise1')
    resolve()
}).then(function () {
    console.log('promsise2')
})
console.log('script end')

输出结果:

script start
async1 start
async2
promise1
script end
async1 end
promsise2
setTimeout

步骤分析:

微信交流群:前端攻城狮 3 群

发表回复