Published at Mar 15 2019
·
0 comments

Instructions

Test suite

Solution

Given a string representing a matrix of numbers, return the rows and columns of that matrix.

So given a string with embedded newlines like:

```
9 8 7
5 3 2
6 6 7
```

representing this matrix:

```
0 1 2
|---------
0 | 9 8 7
1 | 5 3 2
2 | 6 6 7
```

your code should be able to spit out:

- A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
- A list of the columns, reading each column top-to-bottom while moving from left-to-right.

The rows for our example matrix:

- 9, 8, 7
- 5, 3, 2
- 6, 6, 7

And its columns:

- 9, 5, 6
- 8, 3, 6
- 7, 2, 7

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Install assignment dependencies:

```
$ npm install
```

Execute the tests with:

```
$ npm test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by
changing `xtest`

to `test`

.

Warmup to the `saddle-points`

warmup. http://jumpstartlab.com

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import { Matrix } from './matrix';
describe('Matrix', () => {
test('extract row from one number matrix', () => {
expect(new Matrix('1').rows[0]).toEqual([1]);
});
xtest('can extract row', () => {
expect(new Matrix('1 2\n3 4').rows[1]).toEqual([3, 4]);
});
xtest('extract row where numbers have different widths', () => {
expect(new Matrix('1 2\n10 20').rows[1]).toEqual([10, 20]);
});
xtest('can extract row from non-square matrix', () => {
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').rows[2]).toEqual([7, 8, 9]);
});
xtest('extract column from one number matrix', () => {
expect(new Matrix('1').columns[0]).toEqual([1]);
});
xtest('can extract column', () => {
expect(new Matrix('1 2 3\n4 5 6\n7 8 9').columns[2]).toEqual([3, 6, 9]);
});
xtest('can extract column from non-square matrix', () => {
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').columns[2]).toEqual([3, 6, 9, 6]);
});
xtest('extract column where numbers have different widths', () => {
expect(new Matrix('89 1903 3\n18 3 1\n9 4 800').columns[1]).toEqual([1903, 3, 4]);
});
});
```

```
export class Matrix {
constructor(input) {
const toInts = (c) => parseInt(c)
const toRows = (l) => l.split(" ").map(toInts)
this.rows = input.split("\n").map(toRows)
this.columns = [];
for(var i = 0; i < this.rows[0].length; i++) {
this.columns[i] = [];
for(var j = 0; j < this.rows.length; j++) {
this.columns[i][j] = this.rows[j][i];
}
}
}
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,089 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments