Published at Nov 19 2018
·
0 comments

Instructions

Test suite

Solution

Write a function that returns the earned points in a single toss of a Darts game.

Darts is a game where players throw darts to a target.

In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:

- If the dart lands outside the target, player earns no points (0 points).
- If the dart lands in the outer circle of the target, player earns 1 point.
- If the dart lands in the middle circle of the target, player earns 5 points.
- If the dart lands in the inner circle of the target, player earns 10 points.

The outer circle has a radius of 10 units (This is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1. Of course, they are all centered to the same point (That is, the circles are concentric) defined by the coordinates (0, 0).

Write a function that given a point in the target (defined by its `real`

cartesian coordinates `x`

and `y`

), returns the correct amount earned by a dart landing in that point.

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`

.

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

```
import { solve } from './darts';
describe('Return the correct amount earned by a dart landing in a given point in the target problem', () => {
test('A dart lands outside the target', () => {
const x = 15.3;
const y = 13.2;
const expected = 0;
expect(solve(x, y)).toEqual(expected);
});
xtest('A dart lands just in the border of the target', () => {
const x = 10;
const y = 0;
const expected = 1;
expect(solve(x, y)).toEqual(expected);
});
xtest('Input is not a number', () => {
const x = 'WRONG';
const y = 10;
expect(solve(x, y)).toBeNull();
});
xtest('A dart lands in the middle circle', () => {
const x = 3;
const y = 3.7;
const expected = 5;
expect(solve(x, y)).toEqual(expected);
});
xtest('A dart lands right in the border between outside and middle circles', () => {
const x = 0;
const y = 5;
const expected = 5;
expect(solve(x, y)).toEqual(expected);
});
xtest('A dart lands in the inner circle', () => {
const x = 0;
const y = 0;
const expected = 10;
expect(solve(x, y)).toEqual(expected);
});
});
```

```
function solve(x,y) {
if (isNaN(x) || isNaN(y)) return null;
if (x>10 || y>10) return 0;
if (x==10 || y==10) return 1;
if (x==0 && y==0) return 10;
return 5;
}
module.exports = {solve};
```

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,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments