"use strict"
function createIntervals(a) {
let c;
return a.sort((a, b) => a - b)
.reduce((a, b) => (c && b === c[1] + 1 ? c[1] = b : a.push(c = [b, b]), a), []);
}
let output = createIntervals([1, 2, 3, 4, 5, 7, 8, 12]);
alert(JSON.stringify(output)); // output [ [ 1, 5 ], [ 7, 8 ], [ 12, 12 ] ]