# Create string ranges from sorted array of integers

You don't need two loops, just keep track were you started the group:

let array = [1,2,3,4,7,8,12,15,21,21,22,23]

const groups = [];
let start = array[0];

array.push(NaN);

for(let index = 1; index < array.length; index++) {
const value = array[index], previous = array[index - 1];
if(value === previous + 1 || value === previous)
continue;

if(start === previous) {
groups.push("" + previous);
} else {
groups.push(start + "-" + previous);
}
start = value;
}

console.log(groups);

This should do it for you.

const a = [1,2,3,4,7,8,12,15,21,21,22,23,27]

let min = a[0], last = a[0]

const result = a.reduce((arr, n, i) => {
if(n - last > 1){
if(min == last) arr.push(""+min)
else arr.push([min, last].join("-"))

min = n
}

if(i == a.length - 1) {
if(min == n) arr.push(""+min)
else arr.push([min, n].join("-"))
}

last = n

return arr
}, [])

console.log(result)

Another way could be

let a = [1,2,3,4,7,8,12,15,21,21,22,23]

let r = a.reduce((acc, val) => {
const lastGroup = acc.pop() || [];
const lastValue = lastGroup.slice(-1)[0];
if (val - lastValue > 1) {
return [...acc, lastGroup, [val]];
}
return [...acc, [...lastGroup, val]];
}, []).map(group => {
const first = group[0];
const last = group[group.length-1];
return first !== last ? ${first}-${last} : \${first};
});

console.log(r)

You could store the next expected value in a closure and reduce the array.

function getRanges(array) {
return array.reduce((l => (r, v, i, a) => {
if (l[1] > v) return r;
r.push(l[1] === v
? (r.pop(), l.join('-'))
: (l = [v, v]).slice(0, 1).toString()
);
l[1]++;
return r;
})([]), []);
}

console.log(getRanges([-3, -2, -1, 2]));
console.log(getRanges([1, 2, 3, 4, 7, 8, 12, 15, 21, 21, 22, 23]));