function test(arr) {
const arr2 = [];
let temp = [];
let temp2 = [];
arr.forEach((a, i) => {
const conf1 = a - arr[i - 1] === 1
const conf2 = arr[i + 1] - a === 1
if (i > 0) {
if (conf1) {
if (temp.indexOf(arr[i - 1]) < 0) {
temp.push(arr[i - 1])
}
if (temp.indexOf(a) < 0) {
temp.push(a)
}
} else {
temp.length && arr2.push(temp)
!conf2 && arr2.push([a])
temp = []
}
}
})
temp.length && arr2.push(temp)
return arr2.map(v => {
const t = v.length > 1 && v.splice(1, v.length - 2, '->')
return v.join('');
})
}
var result = test([1, 2, 3, 4, 6, 7, 9, 13, 15])
console.log(result)
console.log(test([2, 3, 4, 7, 8, 22, 55, 56, 77, 88, 99, 100]))
console