解释:连续的数一头一尾加“~”,单独的数单独输出
let arra = [1, 2, 3, 5, 7, 8, 10]
function aa (arr) {
let res1 = []
let pre = arr[0]
let next = arr[0]
let j=0
return function () {
for (let i = 0; i < arr.length; i++) {
if (arr[i] + 1 == arr[i + 1]) {
next = arr[i + 1]
}
else {
res1[j]=(pre == next?arr[i]: pre + '~' + next)
pre = next = arr[i + 1] + ""
j++
}
}
console.log(res1.toString())
}
}
aa(arra)()