Check out the following code :
function splitArrayIntoGroups(arr, size) {
// Splitting the array into groups.
var newArray = [];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < size; j++){
newArray.push(arr.splice(0, size));
}
}
var finalResult = [];
for(i = 0; i < newArray.length; i++){
if(newArray[i].length != 0){
finalResult.push(newArray[i]);
}
}
return finalResult;
}
splitArrayIntoGroups([0, 1, 2, 3, 4, 5, 6, 7,8], 2);
The expected output is -
[[0, 1], [2, 3], [4, 5], [6, 7], [8]]
. However, it currently returns [[0, 1], [2, 3], [4, 5], [6, 7]]
. Interestingly, if the input array is changed to ([0,1,2,3,4,5,6,7,8,9,10],2), the code works as intended.
P.S: The focus here is to identify and rectify the issue in this existing code rather than suggesting an entirely different approach or code snippet.