I have to group an given array by sets of 2 using recursion, whenever i return my answer is not in an array
I've created an array then returning that array
input: [5,1,4,2,3]
function pairs(xs) {
if (xs.length <= 1){
return [];
} else {
let [first,second,...rest] = xs;
let result = [first,second];
let newxs = xs.slice(1);
return [result] + pairs(newxs);
}
}
Expected output: [[5, 1], [1, 4], [4, 2], [2, 3]]
Actual output: 5,11,44,22,3
+with arrays. To concatenate arrays you need to useconcat