0 like 0 dislike
79 views
The essence of the question is very simple: given an array [2,4,7,10] how to insert, for example, the number five in order? That is to say that it is between 4 and 7.
| 79 views

0 like 0 dislike
``function insert(n, arr) { let i = 0; while (n > arr[i]) i++; arr.splice(i, 0, n); return arr; }``
by
0 like 0 dislike
``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, <In section 5, 7, 10] h(b) //=> [2, 4, 7, 10, <11>]``
by
0 like 0 dislike
by
0 like 0 dislike
``/* write a function sort in ascending order*/ function sortAsc( a, b ) { return a - b; } const arr = [2, 4, 7, 10]; arr.push(5); /* use the function sort in ascending order*/ arr.sort(sortAsc);``
by
0 like 0 dislike
It is possible here so:
`[2,4,7,10].concat(5).sort((a,b)=>a-b);`
by

0 like 0 dislike