# How to Simplify this code. Creates Rows from Array

How would I simplify this code? WHat Im trying to accomplish is to make my array of numbers into rows based on my variables. In this cased its 5 x 4. Want it to look like this

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

Now I have it and had help here on Stack, but an not 100% sure this is the easier way to do this. Remove the commas and separate into rows and columns

Code

```//Grid Row
const gridRow = 4;
//Grid Column
const gridCol = 5;
// Show Grid
const grid = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"];
/** Pretty-print the grid via function **/
function printGrid() {
let res = "";
for (let r = 0; r < gridRow; r++) {
let srow = "";
for (let c = 0; c < gridCol; c++) {
srow += " " + grid[r * gridCol + c];
}
res += srow.substr(1) + 'n';
}
return res;
}
```

Instead of concatenating strings and then slicing to remove the extra spaces, use `.join()` to concatenate arrays with a delimiter.

You can use `slice()` to extract parts of an array.

```//Grid Row
const gridRow = 4;
//Grid Column
const gridCol = 5;
// Show Grid
const grid = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"];
/** Pretty-print the grid via function **/
function printGrid() {
const resArray = [];
for (let r = 0; r < gridRow; r++) {
const srow = grid.slice(gridCol * r, gridCol * (r + 1)).join(" ");
resArray.push(srow);
}
return resArray.join("n");
}

console.log(printGrid())```