let insert = n => (a, r = [ ], [h, ...t] = a) =>
a.length == 0 ? [n]
: n > a[a.length - 1] ? [...a, n]
: n <= h ? [...r, n, ...a]
: insert(n)(t, [...r, h])
let f = insert(0)
let g = insert(5)
let h = insert(11)
let a = [ ]
let b = [2, 4, 7, 10]
f(a) //=> [<0>]
f(b) //=> [<0>, 2, 4, 7, 10]
g(b) //=> [2, 4, <5>, 7, 10]
h(b) //=> [2, 4, 7, 10, <11>]
function insert(n, arr) {
let i = 0;
while (n > arr[i]) i++;
arr.splice(i, 0, n);
return arr;
}