Skip to content

darts: Add new exercise #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
"stacks"
]
},
{
"slug": "darts",
"uuid": "6c64649b-ea81-4118-9e74-a0a55018ffbc",
"core": false,
"unlocked_by": null,
"difficulty": 3,
"topics": null
},
{
"slug": "hello-world",
"uuid": "9ce0f408-6d7b-4466-a390-75aeaf9492f2",
Expand Down
48 changes: 48 additions & 0 deletions exercises/darts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Darts

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

[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).

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](http://mathworld.wolfram.com/ConcentricCircles.html)) 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.
## Setup

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

[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)

## Requirements

Install assignment dependencies:

```bash
$ npm install
```

## Making the test suite pass

Execute the tests with:

```bash
$ 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`.


## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
44 changes: 44 additions & 0 deletions exercises/darts/darts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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);
});
});
13 changes: 13 additions & 0 deletions exercises/darts/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function solve(x, y) {
// Check for NaN
if (Number.isNaN(Number(x)) || Number.isNaN(Number(y))) return null;

// Use euclidean distance
const distanceToDart = Math.sqrt(x * x + y * y);

// Define points for each section of the target
if (distanceToDart > 10) return 0;
if (distanceToDart > 5) return 1;
if (distanceToDart > 1) return 5;
return 10;
}
80 changes: 80 additions & 0 deletions exercises/darts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "exercism-javascript",
"version": "0.0.0",
"description": "Exercism exercises in Javascript.",
"author": "Katrina Owen",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"babel-jest": "^21.2.0",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-preset-env": "^1.7.0",
"eslint": "^5.6.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"jest": "^23.6.0"
},
"jest": {
"modulePathIgnorePatterns": [
"package.json"
]
},
"babel": {
"presets": [
[
"env",
{
"targets": [
{
"node": "current"
}
]
}
]
],
"plugins": [
[
"babel-plugin-transform-builtin-extend",
{
"globals": [
"Error"
]
}
],
[
"transform-regenerator"
]
]
},
"scripts": {
"test": "jest --no-cache ./*",
"watch": "jest --no-cache --watch ./*",
"lint": "eslint .",
"lint-test": "eslint . && jest --no-cache ./* "
},
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": "airbnb-base",
"rules": {
"import/no-unresolved": "off",
"import/extensions": "off",
"import/prefer-default-export": "off",
"import/no-default-export": "off"
}
},
"license": "MIT",
"dependencies": {}
}