// 1、支持检测递增数字不限出现在任意位置 (确认)
// 2、支持检测递减数字不限出现在任意位置
// 3、支持连续出现的字母不限出现在任意位置
// 4、支持连续出现的数字不限出现在任意位置(确认)
// 5、支持正向连续出现的字母不限出现在任意位置
// 6、支持反向连续出现的字母不限出现在任意位置
// 7、支持检测连续出现的位数 (确认)
function testStr({ str, count, judgeType = false } = {}) {
const len = str.length
const judgeValue = judgeType ? 0 : 1
let flag = false
let fingCount = 0
let recordIndex = -1
for (let index = 0; index < len; index++) {
const prevStr = Number(str[index]);
const nextStr = Number(str[index + 1]);
if (Math.abs(nextStr - prevStr) === judgeValue && index < len - 1 && index - recordIndex === 1) {
fingCount++
recordIndex = index
} else {
fingCount = 0
recordIndex = index
}
if (fingCount === count - 1) {
flag = true
}
}
return flag
}
let a = '@12123456Ab12'
let b = '12@111111@12'
let c = 'a!000000@'
const params
= {
str: a,
count: 6,
judgeType: false
}
const params1
= {
str: b,
count: 6,
judgeType: true
}
const params2
= {
str: c,
count: 6,
judgeType: true
}
console.log(a, testStr(params))
console.log(b, testStr(params))
console.log(c, testStr(params2))
JS 检测输入连续相同字符和递增字符
最新推荐文章于 2023-12-14 13:41:11 发布