1var matrixReshape = function (mat, r, c) {2const origR = mat.length;3const origC = mat[0].length;45if (r * c !== origR * origC) {6return mat;7}89const flat = mat.flatMap((n) => n);1011const output = [];12for (let i = 0; i < flat.length; i++) {13if (i % c === 0) {14output.push([]);15}1617output[output.length - 1].push(flat[i]);18}19return output;20};