I'm trying to use a recursive call to concat a return array
Directions to this problem are: A stream of data is received and needs to be reversed.
Each segment is 8 bits long, meaning the order of these 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.
trying different combinations of things really...
function dataReverse(data) {
//split incoming array into array values consisting of 8 numbers each.
//var octGroups = data.length / 8;
var result = [];
//recursive call
function shuffler(array){
let input = array;
//base case
if(input.length === 0){
return result;
} else {
//concat result with 8 values at a time
let cache = input.splice(-8,8);
result.concat(cache);
return shuffler(input);
}
return result;
}
var reversed = shuffler(data);
//base case is if data.length === 0 return result else
//reverse iterate through array, concating to new return array
//return result
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]));
it is expected to reverse through the input array, concating a result array with 8 values at a time starting at the end, but not reversing the order of the numbers.
My attempt above returns a zero length array. What have I done wrong?