I need to accomplish the following task using recursion:
Declare a function
insert_all_positions
, which takes for arguments: an element, x, and an array, arr. Functions must return an array of arrays, with each array corresponding to arrs with x inserted in a possible position. That is, if arr is the length N, then the result is an array with N + 1 arrays.
For example, the result of insert_all_positions (10, [1,2,3])
is the array:
[[10,1,2,3],
[1,10,2,3],
[1,2,10,3],
[1,2,3,10]]
I have this code so far:
function insert_all_positions (x, arr) {
if (arr.length === 1) {
return arr.concat(x)
}
else {
}
}