面试代码题总结(包含promise和async await)
一、实现unique函数去重
const arr = []
// 生成0-100000之间随机数
for (let i = 0; i < 100; i++) {
// Math.floor 方法用于对数值向下取整
// math.random()*100表示[0,100)之间的随机
arr.push(0 + Math.floor((100 - 0 + 1) * Math.random()))
}
// 要在 Array的原型上添加unique方法
Array.prototype.unique = function () {
// this 就是arr
for (var i = 0; i < this.length; i++) {
for (var j = i + 1; j < this.length; j++) {
if (this[i] === this[j]) {
this.splice(j, 1)
//splice返回数组,这样是把数组存到了另一个数组里面,即数组元素是数组
j--
}
}
}
return this
}
console.log(arr.unique())
二、实现add函数,要去输入结果为300
//剩余参数
function num(...arg) {
console.log(arg)
}
num(1, 2, 3, 4, 5) //[1,2,3,4,5]
console.log(add(100)(50)(70)(80)())
let sum = 0
function add(x) {
if (x) {
sum += x
return add
} else {
return sum
}
}
console.log(add(100)(50)(70)(80)())
三、promise和await
wait 就是promise
await后面的值,就是promise里面的参数
await下面的代码就是promise里面的.then
// new Promise(resolve => {
// console.log('promise1')
// resolve()
// }).then(() => {
// console.log('promise2')
// })
// async function async1() {
// console.log('async1 start')
// Promise.resolve(async2()).then(() => {
// console.log('async1 end')
// })
// }
// async2 = new Promise(function (resolve) {
// console.log('async2')
// resolve()
// }).then(() => {})
//***********************************************************案例
async function async1() {
console.log('async1 start') //2
await async2() //await后面代码会等await内部代码全部完成后再执行
console.log('async1 end') //7
}
async function async2() {
console.log('async2') //3
}
console.log('script start') // 1 同步
setTimeout(function () {
console.log('setTimeout') //8
}, 0)
async1() //-------------------------------2
new Promise(function (resolve) {
console.log('promise1') //4
resolve()
}).then(function () {
console.log('promise2') //6
})
console.log('script end') //5
async function async1() {
console.log('async1 start') //1
return new Promise(resolve => {
resolve(async2())
}).then(() => {
console.log('async1 end') 7
})
}
let async2 = function (resolve) {
return new Promise(function (resolve) {
console.log('async2 start') //2
resolve(fun())
}).then(() => {
console.log('async2 end') //5
})
}
function fun() {
console.log('11111111111') //3
}
async1()
new Promise(function (resolve) {
console.log('promise1') //4
resolve()
}).then(function () {
console.log('promise2') //6
})
//和上面代码一样,只不过是用了async和await
async function async1() {
console.log('async1 start')
await async2() //await后面代码会等await内部代码全部完成后再执行
console.log('async1 end')
}
async function async2() {
console.log('async2 start')
await fun()
console.log('async2 end')
}
function fun() {
console.log('11111111111')
}
async1()
new Promise(function (resolve) {
console.log('promise1')
resolve()
}).then(function () {
console.log('promise2')
})
四、用递归反转一个单链表
// 提前创建一个链表
// 1. 定义 node 结点
const node = (val = null, next = null) => {
return {
value: val,
next: next,
}
}
// 2. 定义链表
var node4 = node('5'),
node3 = node('4', node4),
node2 = node('3', node3),
node1 = node('2', node2),
head = node('1', node1)
// 遍历输出链表
function printList(head) {
var headNode = head,
list = []
while (headNode) {
list.push(headNode)
headNode = headNode.next
}
}
printList(head)
var reverseList = function (head) {
if (head === null || head.next === null) {
return head
}
const headNext = reverseList(head.next)
head.next.next = head
head.next = null
return headNext
}
console.log(reverseList(head))
// 字符串反转
let str = '1->2->3->4->5->NULL'
let newStr = ''
let newArr = []
function reverse(str) {
let arr = str.split('->')
if (arr[0] == 'NULL') {
newArr.push(...arr.splice(0, 1))
return newArr.join('->')
} else {
newArr.unshift(...arr.splice(0, 1))
return reverse(arr.join('->'))
}
}
console.log(reverse('1->2->3->4->5->NULL'))
链接: https://blog.csdn.net/chencl1986/article/details/107601443.