Javascript 验证第一个或最后一个连续且相等的数字

Javascript 验证第一个或最后一个连续且相等的数字,javascript,reactjs,Javascript,Reactjs,我需要验证数字的前两位是否连续或不相等。 这适用于ReactJS项目,使用条件“for”和“if”来实现验证 我有以下代码: for (let i = 0; i < arrayPassword.length - 1; i++) { const actual = parseInt(arrayPassword[i]) const next = parseInt(arrayPassword[i + 1]) //validate consecutive and equa

我需要验证数字的前两位是否连续或不相等。
这适用于ReactJS项目,使用条件“for”和“if”来实现验证

我有以下代码:

for (let i = 0; i < arrayPassword.length - 1; i++) 
{
    const actual = parseInt(arrayPassword[i])
    const next = parseInt(arrayPassword[i + 1])

    //validate consecutive and equal numbers
    let isConsecutive = actual + 1 === next || actual === next ? true : false;

    if (isConsecutive) {
      callback(`consecutive and equal numbers are not allow.`);
    } else {
      callback();
    }
}
这没关系,但当前两个数字相等或连续时就会出现问题,例如:

(1111, 2222, 3333, 1234, 2345, 6789, ...) 
(1147, 2293, 3360, 1244, 6750, 8952, ...)
我希望通过条件,前面示例中的数字不会进入条件If。

非常感谢您对我的帮助。

我相信这个简单的函数可以满足您的需要—检查密码数组中的任意两个相邻数字是否相等或连续

function neighboursAreEqual(arrayPassword) {
    for (var i = 0; i < arrayPassword.length - 1; i++) {
        const current = parseInt(arrayPassword[i]);
        const next = parseInt(arrayPassword[i + 1]);
        if (current === next || Math.abs(current - next) === 1) return true;
    }
    return false;
}

//    to test how it works
console.log(neighboursAreEqual([]))    //    false (no symbols to compare)
console.log(neighboursAreEqual(['1']))    //    false (no neighboring symbol)
console.log(neighboursAreEqual(['1', '2']))    //    true (consecutive)
console.log(neighboursAreEqual(['1', '3']))    //    false
console.log(neighboursAreEqual(['2', '2']))    //    true(equal)
console.log(neighboursAreEqual(['0', '2', '4']))    //    false
console.log(neighboursAreEqual(['0', '2', '3']))    //    true (consecutive)
console.log(neighboursAreEqual(['0', '8', '2', '2']))    //    true  (consecutive)
函数邻域相等(arrayPassword){
for(var i=0;i

如果您只需要比较数组中的前两个符号(尽管我不知道这样做的原因),函数就变得更简单了-您甚至不需要
for
那里…

在查看数字的所有数字之前,无法知道数字是否无效,但您的代码会调用循环中的回调,只看了两个。实际上,回调调用了三次,每次迭代调用一次。要解决此问题,请在单独的变量中跟踪每个有效性检查,并且仅在循环完成后调用回调:

let equalSoFar = true;
let consecutiveSoFar = true;

for (let i = 0; i < arrayPassword.length - 1; i++) {
  const actual = parseInt(arrayPassword[i])
  const next = parseInt(arrayPassword[i + 1])

  // validate equal numbers
  // "equalSoFar &&" because after a nonequal digit is encountered, equalSoFar MUST be false
  equalSoFar = equalSoFar && actual === next;

  // validate consecutive numbers
  // "consecutiveSoFar &&": same logic
  consecutiveSoFar = consecutiveSoFar && actual + 1 === next;
}

if (equalSoFar || consecutiveSoFar) {
  callback(`consecutive and equal numbers are not allowed.`);
} else {
  callback();
}

仅供参考,
让isConsecutive=actual+1==next | | actual==next?真:假是多余的。您可以直接执行
让isConsecutive=actual+1==next | | actual==next是否特别是带有限制的前两位数字?4077有效吗?如果我计算的数字4077没有输入到if条件中,它是可以的,但是如果我计算的数字4470,它输入到if条件中,这是我不希望发生的事情。谢谢你的赞赏。我想我误解了这个问题。要明确的是,唯一不允许的数字是所有四位数字都是连续或相等的数字(11112345),应该是较小的数字,但目前不允许(11471244)。对的(在你的回复中使用“@AuxTaco”,这样它就会显示在我的收件箱中。)@AuxTaco这是正确的。非常感谢你的感谢,但也许我没有给你正确的问题。4位数字集的验证很好,但我也希望在评估4位数字集时,不要解释前两位数字是连续的或相等的,后面跟着两个不连续且不相等的数字。简单地说,它们是连续且相等的条件适用于由4位数字组成的数字。你的描述确实有点不清楚。您的验证规则应该只查看前两位数字,这样说正确吗?这意味着:1175、1275、4596等密码都是无效的(前两位数字相等或连续),但3511、5312、3551、3125等密码是有效的(尽管它们有两对相等或连续的数字,但这些数字不在密码的开头)?然而,您是否只需要按顺序或降序检查“不连续”规则?简单地说,像2135或9835这样的密码有效吗?