How does JS regularity judge six consecutive numbers

for example, 123456, 456789, 876543

Apr.03,2021

directly for loop record the last digit to determine whether the number is continuous


regular is not suitable for this job as upstairs


(0 = 1) | 1 (? = 2) | 2 (? = 3) | 3 (? = 4) | 4 (? = 5) | 5 (? = 6) | 6 (? = 7) | 7 (? = 8) | 8 (?)) {5} d can match positive order
change or reverse order, but judge twice

Let's use a loop, a loop to get it done

function isContinuityNum(num){

  let arr=Array.from(num.toString())
  let arr1= [...arr]
  arr.reverse()
  
  arr3 = arr1.map((n,i)=>{
    return parseInt(n)+parseInt(arr[i])
  })
  
  return [...new Set(arr3)].length === 1

}

var d=isContinuityNum(num)
console.log(d)

regularization needs to be done twice. First, replace 7 digits or more, and then the longest string left is only 6 digits. In this case, you only need to match 6 digits. There is no interference with long numbers.


discuss the importance of Baidu

quoted from: https://bbs.csdn.net/topics/3.

var number = '123456';
var str = number.replace(/\d/g, function($val, pos) {
    return parseInt($val) - pos;
});
console.log(/^(\d)\1+$/.test(str));

var number = '654321';
var str = number.replace(/\d/g, function($val, pos) {
    return parseInt($val) + pos;
});
console.log(/^(\d)\1+$/.test(str));
Menu