拼多多前端暑期实习笔试
选择题:
1. promise执行顺序问题
promise executor里的console —— catch之后的console——rejected的promise没有res,因此reference error
2. const和参数形参实参问题
问答(综合编程题):
1. 字符串重复次数最多字符
思路一:map
思路二:字符串转为数组,排序后根据字符firstindex和lastindex计算重复次数
参考:https://cloud.tencent.com/developer/article/1839507
2. 异步任务调度
function asyncRetry(fn, times, timeout = 0) { // fn必需 // times必需,且为大于0的正整数 times = parseInt(times); if(times <= 0 || isNaN(times)) { throw new TypeError("times必需是大于0的正整数"); } // Number.isInteger() return Promise.race([ // 错误后重复 new Promise(async (resolve, reject) => { while(times--) { try { const res = await fn(); resolve(res); break; } catch (error) { if(times <= 0) { reject("retry error"); } } } }), // 超时任务 new Promise((resolve, reject) => { setTimeout(() => { reject("retry error"); }, timeout) }) ]); } // https://blog.csdn.net/xy1580/article/details/127285053 // https://juejin.cn/post/6971411461767692325#heading-7
题目要求输出结果or rejected的promise,上面是返回promise。
3.抽奖