JS 检测输入连续相同字符和递增字符

        // 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))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值