🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ScreamingPeter

ScreamingPeter's solution

to Darts in the JavaScript Track

Published at Oct 17 2020 · 0 comments
Instructions
Test suite
Solution

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

Please cd into exercise directory before running all below commands.

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 Solutions

Once you have a solution ready, you can submit it using:

exercism submit darts.js

Submitting Incomplete Solutions

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

Exercise Source Credits

Inspired by an exercise created by a professor Della Paolera in Argentina

darts.spec.js

import { score } from './darts';

describe('Darts', () => {
  test('Missed target', () => {
    expect(score(-9, 9)).toEqual(0);
  });

  xtest('On the outer circle', () => {
    expect(score(0, 10)).toEqual(1);
  });

  xtest('On the middle circle', () => {
    expect(score(-5, 0)).toEqual(5);
  });

  xtest('On the inner circle', () => {
    expect(score(0, -1)).toEqual(10);
  });

  xtest('Exactly on centre', () => {
    expect(score(0, 0)).toEqual(10);
  });

  xtest('Near the centre', () => {
    expect(score(-0.1, -0.1)).toEqual(10);
  });

  xtest('Just within the inner circle', () => {
    expect(score(0.7, 0.7)).toEqual(10);
  });

  xtest('Just outside the inner circle', () => {
    expect(score(0.8, -0.8)).toEqual(5);
  });

  xtest('Just within the middle circle', () => {
    expect(score(-3.5, 3.5)).toEqual(5);
  });

  xtest('Just outside the middle circle', () => {
    expect(score(-3.6, -3.6)).toEqual(1);
  });

  xtest('Just within the outer circle', () => {
    expect(score(-7.0, 7.0)).toEqual(1);
  });

  xtest('Just outside the outer circle', () => {
    expect(score(7.1, -7.1)).toEqual(0);
  });

  xtest('Asymmetric position between the inner and middle circles', () => {
    expect(score(0.5, -4)).toEqual(5);
  });
});
//
// This is only a SKELETON file for the 'Darts' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const score = (x,y) => {
  if (Math.abs(Math.pow(x, 2)) + Math.abs(Math.pow(y, 2)) > 100){
      return 0;
  } else if (Math.abs(Math.pow(x, 2)) + Math.abs(Math.pow(y, 2)) > 25){
      return 1;
  } else if (Math.abs(Math.pow(x, 2)) + Math.abs(Math.pow(y, 2)) > 1){
      return 5;
  } else {return 10;
  }
};

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?