Avatar of brunnock

brunnock's solution

to Darts in the JavaScript Track

Published at Nov 19 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

Setup

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

https://exercism.io/tracks/javascript/installation

Requirements

Install assignment dependencies:

$ npm install

Making the test suite pass

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.

Submitting Incomplete Solutions

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

darts.spec.js

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);
  });
});
function solve(x,y) {
  if (isNaN(x) || isNaN(y)) return null;
  if (x>10 || y>10) return 0;
  if (x==10 || y==10) return 1;
  if (x==0 && y==0) return 10;
  return 5;
}

module.exports = {solve};

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?