我有一串数字,其中总是有一个随机序列,比如:
891011 -> 8, 9, 10, 11
123124 -> 123, 124
我怎么才能找到这个序列呢?我当前的代码是:
var digits = [8, 9, 1, 0, 1, 1];
var number;
for (var j = 0; j < digits.length; j++) {
number = digits.slice(0, j + 1).join("");
if (
Number(number) === Number(digits.slice(j, number.length).join("")) &&
digits
.join("")
.match(new RegExp(".{1," + number.length + "}", "g"))
.every(function(num, index) {
return index === digits.length - 1 || num < digits[index + 1];
})
) {
break;
}
}
console.log(number)
1条答案
按热度按时间cngwdvgl1#
您可以增加起始数字的位数,看看是否可以生成与连续数字匹配的输出。尝试如下所示: