I have got a little function in java script and I want to split an array A into a 2d array(square array).
function twoDimensional(array, row) {
let newArray = [];
let arraySize = Math.floor(array.length / row);
let extraArraySize = array.length % row;
while (array.length) {
if (!!extraArraySize) {
newArray.push(array.splice(0, arraySize + 1));
extraArraySize--;
} else {
newArray.push(array.splice(0, arraySize));
}
}
return newArray;
}
I want to get an array from input and converts it to a square array.
Matrices should be padded on the right or at the bottom, but not both simultaneously (for example the size of the biggest direction shouldn't change).
If the input is already a square matrix, just return that matrix.
for example:
Input: [1,2] => output : [[1,2],[0,0]]
Input: [[6,2],[4,9]] => output : [[6,2],[4,9]]
Input: [[3,2],[9,9],[4,8]] => output : [[3,2,0],[9,9,0],[4,8,0]]
could you help me How I can change my Function to get above outputs?
any solutions would be my appreciated