前端面试高频笔试题,前端面试要做到提前准备提前练习,刷一定的面试题笔试题量,面试才能事半功倍一路畅通。
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]
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
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))
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 startasync1 startasync2promise1script endasync1 endpromsise2setTimeout
原文出处:前端面试笔试题-高频