Published at Aug 25 2019
·
1 comment

Instructions

Test suite

Solution

Parse and evaluate simple math word problems returning the answer as an integer.

Problems with no operations simply evaluate to the number given.

What is 5?

Evaluates to 5.

Add two numbers together.

What is 5 plus 13?

Evaluates to 18.

Handle large numbers and negative numbers.

Now, perform the other three operations.

What is 7 minus 5?

2

What is 6 multiplied by 4?

24

What is 25 divided by 5?

5

Handle a set of operations, in sequence.

Since these are verbal word problems, evaluate the expression from
left-to-right, *ignoring the typical order of operations.*

What is 5 plus 13 plus 6?

24

What is 3 plus 2 multiplied by 3?

15 (i.e. not 9)

The parser should reject:

- Unsupported operations ("What is 52 cubed?")
- Non-math questions ("Who is the President of the United States")
- Word problems with invalid syntax ("What is 1 plus plus 2?")

If you'd like, handle exponentials.

What is 2 raised to the 5th power?

32

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

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

Install assignment dependencies:

```
$ yarn install
```

Execute the tests with:

```
$ yarn 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 `xit`

to
`it`

.

Inspired by one of the generated questions in the Extreme Startup game. https://github.com/rchatley/extreme_startup

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

```
import { WordProblem, ArgumentError } from './wordy'
describe('Word Problem', () => {
it('add 1', () => {
const question = 'What is 1 plus 1?'
expect(new WordProblem(question).answer()).toEqual(2)
})
xit('add 2', () => {
const question = 'What is 53 plus 2?'
expect(new WordProblem(question).answer()).toEqual(55)
})
xit('add negative numbers', () => {
const question = 'What is -1 plus -10?'
expect(new WordProblem(question).answer()).toEqual(-11)
})
xit('add more digits', () => {
const question = 'What is 123 plus 45678?'
expect(new WordProblem(question).answer()).toEqual(45801)
})
xit('subtract', () => {
const question = 'What is 4 minus -12?'
expect(new WordProblem(question).answer()).toEqual(16)
})
xit('multiply', () => {
const question = 'What is -3 multiplied by 25?'
expect(new WordProblem(question).answer()).toEqual(-75)
})
xit('divide', () => {
const question = 'What is 33 divided by -3?'
expect(new WordProblem(question).answer()).toEqual(-11)
})
xit('add twice', () => {
const question = 'What is 1 plus 1 plus 1?'
expect(new WordProblem(question).answer()).toEqual(3)
})
xit('add then subtract', () => {
const question = 'What is 1 plus 5 minus -2?'
expect(new WordProblem(question).answer()).toEqual(8)
})
xit('subtract twice', () => {
const question = 'What is 20 minus 4 minus 13?'
expect(new WordProblem(question).answer()).toEqual(3)
})
xit('subtract then add', () => {
const question = 'What is 17 minus 6 plus 3?'
expect(new WordProblem(question).answer()).toEqual(14)
})
xit('multiply twice', () => {
const question = 'What is 2 multiplied by -2 multiplied by 3?'
expect(new WordProblem(question).answer()).toEqual(-12)
})
xit('add then multiply', () => {
const question = 'What is -3 plus 7 multiplied by -2?'
expect(new WordProblem(question).answer()).toEqual(-8)
})
xit('divide twice', () => {
const question = 'What is -12 divided by 2 divided by -3?'
expect(new WordProblem(question).answer()).toEqual(2)
})
xit('too advanced', () => {
const question = 'What is 53 cubed?'
const problem = new WordProblem(question)
expect(problem.answer.bind(problem)).toThrowError( ArgumentError )
})
xit('irrelevant', () => {
const question = 'Who is the president of the United States?'
const problem = new WordProblem(question)
expect(problem.answer.bind(problem)).toThrowError( ArgumentError )
})
})
```

```
interface QuestionPart {
text: string;
number: number
}
class WordProblem {
private questionParts:Array<QuestionPart> = [];
constructor(private question:string) {
}
public answer():number {
this.parse(this.question);
let result:number;
if (this.questionParts.length && this.questionParts[0].text === 'What is') {
result = this.questionParts[0].number;
this.questionParts.shift();
}
else {
throw new ArgumentError('Not a question!');
}
this.questionParts.forEach(({text, number}) => {
switch (text) {
case 'plus':
result += number;
break;
case 'minus':
result -= number;
break;
case 'multiplied by':
result *= number;
break;
case 'divided by':
result /= number;
break;
default:
throw new ArgumentError('Operation invalid!');
}
});
return result;
}
private parse(question:string):void {
const regexp:RegExp = /^(What is|plus|minus|multiplied by|divided by) (-?\d+) ?/;
let match:string[]|null = question.match(regexp) || null;
let text:string = question;
while (match !== null && match.length === 3) {
text = text.substr(match[0].length);
this.questionParts.push({
text: match[1],
number: parseInt(match[2])
});
match = text.match(regexp);
}
if (text !== '?') {
throw new ArgumentError('I do not understand!');
}
}
}
class ArgumentError extends Error{}
export { WordProblem, ArgumentError };
```

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

The last two tests gave me quite a headache until I realized that I did not throw ArgumentErrors in answer()-method but in constructor (i wanted to do parse() here).