From 91a02f89a555c159ee1e45e71620bc03648797d2 Mon Sep 17 00:00:00 2001 From: ramadis Date: Tue, 9 Oct 2018 21:16:53 +0200 Subject: [PATCH 1/6] Add exercise 'target' --- exercises/target/README.md | 49 ++++++++++++++++++++ exercises/target/example.js | 17 +++++++ exercises/target/package.json | 80 +++++++++++++++++++++++++++++++++ exercises/target/target.spec.js | 44 ++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 exercises/target/README.md create mode 100644 exercises/target/example.js create mode 100644 exercises/target/package.json create mode 100644 exercises/target/target.spec.js diff --git a/exercises/target/README.md b/exercises/target/README.md new file mode 100644 index 0000000000..1592d9f082 --- /dev/null +++ b/exercises/target/README.md @@ -0,0 +1,49 @@ +# Target + +Write a function that returns the earned points in 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 amount 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 [concentrics](http://mathworld.wolfram.com/ConcentricCircles.html)). + +Write a function that given a point in the target (defined by its `real` cartesian coordinates `x` and `y`), returns the correct amount earnt 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/target/example.js b/exercises/target/example.js new file mode 100644 index 0000000000..1a8531d94d --- /dev/null +++ b/exercises/target/example.js @@ -0,0 +1,17 @@ +export default function solve(x, y) { + // Define as numbers + x = Number(x); + y = Number(y); + + // Check for NaN + if (x !== x || y !== 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; + else if (distanceToDart > 5) return 1; + else if (distanceToDart > 1) return 5; + return 10; +} \ No newline at end of file diff --git a/exercises/target/package.json b/exercises/target/package.json new file mode 100644 index 0000000000..f625e85896 --- /dev/null +++ b/exercises/target/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.4.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": {} +} diff --git a/exercises/target/target.spec.js b/exercises/target/target.spec.js new file mode 100644 index 0000000000..a416bab446 --- /dev/null +++ b/exercises/target/target.spec.js @@ -0,0 +1,44 @@ +import solve from './target'; + +describe('Solve 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); + }); + + test('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); + }); + + test('Input is not a number', () => { + const x = 'WRONG'; + const y = 10; + expect(solve(x, y)).toBeNull(); + }); + + test('A dart lands in the middle circle', () => { + const x = 3; + const y = 3.7; + const expected = 5; + expect(solve(x, y)).toEqual(expected); + }); + + test('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); + }); + + test('A dart arrives in the inner circle', () => { + const x = 0; + const y = 0; + const expected = 10; + expect(solve(x, y)).toEqual(expected); + }); +}); From f761db42f4e885e9720e8d066a3b1099180721ba Mon Sep 17 00:00:00 2001 From: ramadis Date: Tue, 23 Oct 2018 21:00:29 +0200 Subject: [PATCH 2/6] Several fixes: Problem specification + Style --- config.json | 8 ++++++++ exercises/target/README.md | 9 ++++----- exercises/target/example.js | 14 +++++--------- exercises/target/package.json | 2 +- exercises/target/target.spec.js | 10 +++++----- 5 files changed, 23 insertions(+), 20 deletions(-) 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/target/README.md b/exercises/target/README.md index 1592d9f082..000945cf8b 100644 --- a/exercises/target/README.md +++ b/exercises/target/README.md @@ -1,21 +1,20 @@ # Target -Write a function that returns the earned points in a Darts game. +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 amount of points, depending on where the dart lands: +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 [concentrics](http://mathworld.wolfram.com/ConcentricCircles.html)). - -Write a function that given a point in the target (defined by its `real` cartesian coordinates `x` and `y`), returns the correct amount earnt by a dart landing in that point. +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 diff --git a/exercises/target/example.js b/exercises/target/example.js index 1a8531d94d..c425c34ceb 100644 --- a/exercises/target/example.js +++ b/exercises/target/example.js @@ -1,17 +1,13 @@ export default function solve(x, y) { - // Define as numbers - x = Number(x); - y = Number(y); - // Check for NaN - if (x !== x || y !== y) return null; + if (Number.isNaN(x) || Number.isNaN(y)) return null; // Use euclidean distance - const distanceToDart = Math.sqrt(x*x + y*y); + const distanceToDart = Math.sqrt(x * x + y * y); // Define points for each section of the target if (distanceToDart > 10) return 0; - else if (distanceToDart > 5) return 1; - else if (distanceToDart > 1) return 5; + if (distanceToDart > 5) return 1; + if (distanceToDart > 1) return 5; return 10; -} \ No newline at end of file +} diff --git a/exercises/target/package.json b/exercises/target/package.json index f625e85896..7621f040d5 100644 --- a/exercises/target/package.json +++ b/exercises/target/package.json @@ -1,6 +1,6 @@ { "name": "exercism-javascript", - "version": "0.0.0", + "version": "1.0.0", "description": "Exercism exercises in Javascript.", "author": "Katrina Owen", "private": true, diff --git a/exercises/target/target.spec.js b/exercises/target/target.spec.js index a416bab446..92cb5fe719 100644 --- a/exercises/target/target.spec.js +++ b/exercises/target/target.spec.js @@ -8,34 +8,34 @@ describe('Solve the target problem', () => { expect(solve(x, y)).toEqual(expected); }); - test('A dart lands just in the border of the target', () => { + 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); }); - test('Input is not a number', () => { + xtest('Input is not a number', () => { const x = 'WRONG'; const y = 10; expect(solve(x, y)).toBeNull(); }); - test('A dart lands in the middle circle', () => { + xtest('A dart lands in the middle circle', () => { const x = 3; const y = 3.7; const expected = 5; expect(solve(x, y)).toEqual(expected); }); - test('A dart lands right in the border between outside and middle circles', () => { + 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); }); - test('A dart arrives in the inner circle', () => { + xtest('A dart lands in the inner circle', () => { const x = 0; const y = 0; const expected = 10; From 4e4194d71de45cae35bb33c1138575544ffedb7d Mon Sep 17 00:00:00 2001 From: ramadis Date: Tue, 23 Oct 2018 21:02:23 +0200 Subject: [PATCH 3/6] Some other improvements --- exercises/target/README.md | 2 +- exercises/target/target.spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/target/README.md b/exercises/target/README.md index 000945cf8b..df07725be6 100644 --- a/exercises/target/README.md +++ b/exercises/target/README.md @@ -1,4 +1,4 @@ -# Target +# Darts Write a function that returns the earned points in a single toss of a Darts game. diff --git a/exercises/target/target.spec.js b/exercises/target/target.spec.js index 92cb5fe719..11d16d3437 100644 --- a/exercises/target/target.spec.js +++ b/exercises/target/target.spec.js @@ -1,6 +1,6 @@ import solve from './target'; -describe('Solve the target problem', () => { +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; From 9b43247d427ee01cd495dff4e50d4b13caeb233c Mon Sep 17 00:00:00 2001 From: ramadis Date: Tue, 23 Oct 2018 22:05:37 +0200 Subject: [PATCH 4/6] Rename folder. Update package.json --- exercises/{target => darts}/README.md | 0 exercises/{target => darts}/example.js | 0 exercises/{target => darts}/package.json | 2 +- exercises/{target => darts}/target.spec.js | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename exercises/{target => darts}/README.md (100%) rename exercises/{target => darts}/example.js (100%) rename exercises/{target => darts}/package.json (98%) rename exercises/{target => darts}/target.spec.js (100%) diff --git a/exercises/target/README.md b/exercises/darts/README.md similarity index 100% rename from exercises/target/README.md rename to exercises/darts/README.md diff --git a/exercises/target/example.js b/exercises/darts/example.js similarity index 100% rename from exercises/target/example.js rename to exercises/darts/example.js diff --git a/exercises/target/package.json b/exercises/darts/package.json similarity index 98% rename from exercises/target/package.json rename to exercises/darts/package.json index 7621f040d5..f625e85896 100644 --- a/exercises/target/package.json +++ b/exercises/darts/package.json @@ -1,6 +1,6 @@ { "name": "exercism-javascript", - "version": "1.0.0", + "version": "0.0.0", "description": "Exercism exercises in Javascript.", "author": "Katrina Owen", "private": true, diff --git a/exercises/target/target.spec.js b/exercises/darts/target.spec.js similarity index 100% rename from exercises/target/target.spec.js rename to exercises/darts/target.spec.js From a7466ac3e0ab0906c5aec5bb92803bc4e8de88a9 Mon Sep 17 00:00:00 2001 From: ramadis Date: Wed, 24 Oct 2018 13:15:35 +0200 Subject: [PATCH 5/6] Fixes for all test to pass --- exercises/darts/{target.spec.js => darts.spec.js} | 2 +- exercises/darts/example.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename exercises/darts/{target.spec.js => darts.spec.js} (97%) diff --git a/exercises/darts/target.spec.js b/exercises/darts/darts.spec.js similarity index 97% rename from exercises/darts/target.spec.js rename to exercises/darts/darts.spec.js index 11d16d3437..f37a466f62 100644 --- a/exercises/darts/target.spec.js +++ b/exercises/darts/darts.spec.js @@ -1,4 +1,4 @@ -import solve from './target'; +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', () => { diff --git a/exercises/darts/example.js b/exercises/darts/example.js index c425c34ceb..703ef1708e 100644 --- a/exercises/darts/example.js +++ b/exercises/darts/example.js @@ -1,6 +1,6 @@ export default function solve(x, y) { // Check for NaN - if (Number.isNaN(x) || Number.isNaN(y)) return null; + if (Number.isNaN(Number(x)) || Number.isNaN(Number(y))) return null; // Use euclidean distance const distanceToDart = Math.sqrt(x * x + y * y); From 440e1932fb6361273e469af9cc55dcfd741da50c Mon Sep 17 00:00:00 2001 From: ramadis Date: Sun, 28 Oct 2018 14:28:33 +0100 Subject: [PATCH 6/6] Fix package.json diff --- exercises/darts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/darts/package.json b/exercises/darts/package.json index f625e85896..af94361caa 100644 --- a/exercises/darts/package.json +++ b/exercises/darts/package.json @@ -12,7 +12,7 @@ "babel-eslint": "^10.0.1", "babel-jest": "^21.2.0", "babel-plugin-transform-builtin-extend": "^1.1.2", - "babel-preset-env": "^1.4.0", + "babel-preset-env": "^1.7.0", "eslint": "^5.6.0", "eslint-config-airbnb-base": "^13.1.0", "eslint-plugin-import": "^2.14.0",