-
-
Notifications
You must be signed in to change notification settings - Fork 628
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
darts: Add new exercise #498
Changes from 1 commit
91a02f8
f761db4
4e4194d
9b43247
a7466ac
440e193
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's be more semantic and validate as early as possible, before the if (isNaN(x) || isNaN(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; | ||
} |
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.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": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All tests but the first should be disabled with |
||
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should avoid re-assigning the input params. I'm surprised the linter didn't complain about this. We're currently using the Airbnb style guide: http://airbnb.io/javascript/#functions--mutate-params