ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Oct 18 2020
·
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

Please `cd`

into exercise directory before running all below commands.

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`

.

Once you have a solution ready, you can submit it using:

```
exercism submit darts.js
```

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

Inspired by an exercise created by a professor Della Paolera in Argentina

```
import { score } from './darts';
describe('Darts', () => {
test('Missed target', () => {
expect(score(-9, 9)).toEqual(0);
});
xtest('On the outer circle', () => {
expect(score(0, 10)).toEqual(1);
});
xtest('On the middle circle', () => {
expect(score(-5, 0)).toEqual(5);
});
xtest('On the inner circle', () => {
expect(score(0, -1)).toEqual(10);
});
xtest('Exactly on centre', () => {
expect(score(0, 0)).toEqual(10);
});
xtest('Near the centre', () => {
expect(score(-0.1, -0.1)).toEqual(10);
});
xtest('Just within the inner circle', () => {
expect(score(0.7, 0.7)).toEqual(10);
});
xtest('Just outside the inner circle', () => {
expect(score(0.8, -0.8)).toEqual(5);
});
xtest('Just within the middle circle', () => {
expect(score(-3.5, 3.5)).toEqual(5);
});
xtest('Just outside the middle circle', () => {
expect(score(-3.6, -3.6)).toEqual(1);
});
xtest('Just within the outer circle', () => {
expect(score(-7.0, 7.0)).toEqual(1);
});
xtest('Just outside the outer circle', () => {
expect(score(7.1, -7.1)).toEqual(0);
});
xtest('Asymmetric position between the inner and middle circles', () => {
expect(score(0.5, -4)).toEqual(5);
});
});
```

```
export const score = (x, y) => {
const dartRadius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
const DIMENSIONS = {
outer: 10,
middle: 5,
inner: 1,
};
const POINTS = {
outer: 1,
middle: 5,
inner: 10,
};
if (dartRadius <= DIMENSIONS.inner) return POINTS.inner;
if (dartRadius <= DIMENSIONS.middle) return POINTS.middle;
if (dartRadius <= DIMENSIONS.outer) return POINTS.outer;
return 0;
};
```

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

## Community comments