js 判断 数字数组 是否 连续
预期
[1,2,3,4,5] => true
[1,2,3,5,6] => false
代码
//判断一串数字是否是连续的
const isContinuityNum = (num: number[] | number) => {
let array = [];
if (num instanceof Array) {
array = [...num];
} else {
array = Array.from(num.toString()); //转换为数组
}
let i = array[0];
let isContinuation = true;
for (let e in array) {
if (array[e] != i) {
isContinuation = false;
break;
}
i++;
}
return isContinuation;
};
测试