Question: What are the exact steps that this matrix function does to return the columns for input `'1 2 3\n4 5 6'`?

```
function Matrix(input) {
  const thisRows =
    input.split('\n').map(row => row.split(' ').map(Number));

  return {
    rows: thisRows,
    columns: thisRows[0].map((col,i) => thisRows.map(row => row[i]))
  }
}

```

I don't understand how `thisRows[0].map((col,i) => thisRows.map(row => row[i]))` returns the columns correctly, more specifically, why we use `thisRows[0]`.

Answer: 
The input for this function will be a string, like 

```
'1 2 3\n4 5 6'

```

So, the function splits on the \n, returning

```
['1 2 3', '4 5 6']

```

Then, the function loops over each item in the array, and splits the item on each `' '` and converting each subitem to a number, returning

```
[[1, 2, 3], [4, 5, 6]]

```

The function then grabs the first row **to get the width of the matrix**. This row is not used, but the index inside the loop is. Then, inside the loop, you loop over each row and grab the element at the specified index, returning the columns, rather than rows.

For example. In the first loop, the index is 0. Inside the second loop, we will loop over each row and grab the 0 indexed element:

```js
i = 0
// first iteration
[1, 2, 3][i] = 1
// second iteration
[4, 5, 6][i] = 4

=> [1, 4]

```

then, the next index will be 1, so the same thing happens:

```js
i = 1
// first iteration
[1, 2, 3][i] = 2
// second iteration
[4, 5, 6][i] = 5

=> [2, 5]

```

and so on. Then, once both loops have finished, all of the columns will be returned in an array:

```
[[1, 4], [2, 5], [3, 6]]

```

Here's an example, with some console.logs to show what I mean:

```js
function Matrix(input) {
  const thisRows =
    input.split('\n').map(row => row.split(' ').map(Number));

  return {
    rows: thisRows,
    columns: thisRows[0].map((col,i) => {
      console.log('index:', i)
      return thisRows.map(row => {
        console.log('row:', JSON.stringify(row), '\nel:', row[i])
        return row[i]
      })
    })
  }
}

console.log(Matrix('1 2 3\n4 5 6'))
```
After 

```
const thisRows =
    input.split('\n').map(row => row.split(' ').map(Number));

```

you have rows: `[ [1,2,3], [4,5,6] ]`

Now to get columns, you want to iterate over first row and use index of current value in map function to get values from every row in another map:

```
i=0 [1,4],
i=1 [2,5],
i=2 [3,6]

```