02 Code
001 用递归算法实现,数组长度为 5 且元素的随机数在 2-32 间不重复的值
function rand(arr = [], length = 5, min = 2, max = 32) {
if (arr.length < length) {
const num = Math.floor(Math.random() * (max - min + 1)) + min
if (!arr.includes(num)) {
arr.push(num)
}
return rand(arr)
}
return arr
}002 去除字符串中的空格,根据传入不同的类型分别能去掉前、后、前后、中间的空格
const POSITION = Object.freeze({
left: Symbol(),
right: Symbol(),
both: Symbol(),
center: Symbol(),
all: Symbol(),
})
function trim(str, position = POSITION.both) {
switch (position) {
case POSITION.left:
str = str.replace(/^\s+/, '')
break
case POSITION.right:
str = str.replace(/\s+$/, '')
break
case POSITION.both:
str = str.replace(/^\s+|\s+$/g, '')
break
case POSITION.center:
while (str.match(/\w\s+\w/)) {
str = str.replace(/(\w)(\s+)(\w)/, '$1$3')
}
break
case POSITION.all:
str = str.replace(/\s/g, '')
break
default:
break
}
return str
}003 去除字符串中最后一个指定的字符
004 设计实现 Promise.finally
005 设计实现 Promise.race
006 子字符串匹配算法
007 add(1)(2, 3) 链式调用函数
008 如何快速从一个巨大的数组中随机获取部分元素
009 实现一个批量请求函数 multiRequest(urls, maxNum)
010 实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject
011 自增 ID
最后更新于