for (var a = [6, 7, 8, 14, 15, 16, 20, 21], c = [], temp, i = 0; i < a.length; i++) if (!temp && (temp = a[i]), void 0 !== a[i + 1] && 1 < a[i + 1] - a[i] || i == a.length - 1) c.push(temp + " - " + a[i]), temp = !1; alert(c);