拼多多3.30前端笔试
两道选择,3道编程(Markdown编辑器,纯手撕,太难受了)
1.promise输出结果
const getPromise = ()=>{ return new Promise((resolve,reject)=>{ console.log('a new promise start') reject('error') console.log('a new promise end') }) } async function run(){ const promise = getPromise() promise.catch(console.log) const res = await promise console.log(res) } run()
2.代码执行结果
function modifyPerson(person){ person.age = 2 person = {age: 3} return person } const person = {age:1} modifyPerson(person) console.log(person)
3.找出字符串中连续出现最多的字符和对应的出现个数
const arr = str.match(/(.)\1+/g); const maxLen = Math.max(...arr.map(s => s.length)); const result = arr.reduce((pre, curr) => { if (curr.length === maxLen) { pre[curr[0]] = curr.length } return pre; }, {}); console.log(result) // {c: 3}
4.可指定超时时间的异步函数重试机制
function asyncFn() { return new Promise((resolve, reject) => { setTimeout(() => { if (Math.random() < 0.2) { console.log('inner success') resolve('success') } else { console.log('inner failed') reject('failed') } }, 5 * 1000 * Math.random()) }) } function runWithRetry(fn, retryTimes, timeout) { return new Promise((resolve, reject) => { const tryFn = () => { fn() .then((result) => { resolve(result); }) .catch((error) => { if (retryTimes <= 0 || timeout <= 0) { reject(error); } else { console.log(`Retrying ${retryTimes} more times...`); setTimeout(() => { tryFn(); }, 5 * 1000 * Math.random()); retryTimes--; timeout -= 5 * 1000 * Math.random(); } }); }; tryFn(); }); } runWithRetry(asyncFn, 3, 10 * 1000).then(console.log, console.log)
5.补充css布局代码和Js代码,实现简易的九宫格抽奖功能(这波操作很拼多多