I am attempting to reverse a stream of data using a recursive call to concatenate a return array.
The instructions for this problem are as follows: The incoming data needs to be reversed in segments that are 8 bits long.
This means that the order of these 8-bit segments needs to be reversed. For example:
11111111 00000000 00001111 10101010 (byte1) (byte2) (byte3) (byte4) should become:
10101010 00001111 00000000 11111111 (byte4) (byte3) (byte2) (byte1)
The total number of bits will always be a multiple of 8.
I have been experimenting with different combinations of approaches...
function dataReverse(data) {
//split the incoming array into values consisting of 8 numbers each.
var result = [];
function shuffler(array){
let input = array;
if(input.length === 0){
return result;
} else {
let cache = input.splice(-8,8);
result.concat(cache);
return shuffler(input);
}
return result;
}
var reversed = shuffler(data);
return reversed;
}
console.log(dataReverse([1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0]));
The expected behavior is to reverse through the input array by concatenating a result array with 8 values at a time starting from the end, without changing the order of the numbers.
My implementation above is returning an empty array. What mistake have I made?