diff --git a/config.json b/config.json index 569be4c8a5..e73f1bddd8 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/darts/README.md b/exercises/darts/README.md new file mode 100644 index 0000000000..df07725be6 --- /dev/null +++ b/exercises/darts/README.md @@ -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. diff --git a/exercises/darts/darts.spec.js b/exercises/darts/darts.spec.js new file mode 100644 index 0000000000..f37a466f62 --- /dev/null +++ b/exercises/darts/darts.spec.js @@ -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); + }); +}); diff --git a/exercises/darts/example.js b/exercises/darts/example.js new file mode 100644 index 0000000000..703ef1708e --- /dev/null +++ b/exercises/darts/example.js @@ -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; +} diff --git a/exercises/darts/package.json b/exercises/darts/package.json new file mode 100644 index 0000000000..af94361caa --- /dev/null +++ b/exercises/darts/package.json @@ -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": {} +}