There is an array, the numbers are like the number of minibuses on this street. Numbers go only to increase and in order.
[1,3,4,5,6,8,10,12,13,14,17]
It is necessary that those numbers that are consistent with each other by 1 -
(3,4,5,6) (12,13,14)
were written this way -
(3-6) (12-14)
And in the end get such a String -
1,3-6,8,10,12-14,17
More array examples
[10,11,12] 10-12
[1,2,3,4,5,9] 1-5,9
I have an inferior code.
function solution(x) { let y = []; let min; let max; for (var i = 0; i < x.length; i++) { if (x[i] - x[i - 1] == 1) { y.push(x[i]) } } y.unshift(y[0] - 1); min = y[0]; max = y[y.length - 1]; y = [min, max]; y = y.toString().split(",").join("-"); return y } console.log(solution([1, 3, 4, 5, 6, 8])) //3-6 console.log(solution([1, 3, 4, 5, 6, 8, 10, 11, 12])) //3-12