summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-09-06 22:19:21 +0530
committerAndinus <andinus@nand.sh>2021-09-06 22:19:21 +0530
commit5622b92d80a80faabc1bcf35c94a35a28ff63e05 (patch)
tree40f501577ae8929508bf05c33069e651a746afb8
parent2c3463a677d01dfc027bc518de91eeeaebc14a5b (diff)
downloadexercism-5622b92d80a80faabc1bcf35c94a35a28ff63e05.tar.gz
JS: Triangle: Add solution
-rw-r--r--javascript/triangle/.eslintrc14
-rw-r--r--javascript/triangle/.npmrc1
-rw-r--r--javascript/triangle/HELP.md73
-rw-r--r--javascript/triangle/LICENSE21
-rw-r--r--javascript/triangle/README.md49
-rw-r--r--javascript/triangle/babel.config.js15
-rw-r--r--javascript/triangle/package.json31
-rw-r--r--javascript/triangle/triangle.js43
-rw-r--r--javascript/triangle/triangle.spec.js104
9 files changed, 351 insertions, 0 deletions
diff --git a/javascript/triangle/.eslintrc b/javascript/triangle/.eslintrc
new file mode 100644
index 0000000..1d44460
--- /dev/null
+++ b/javascript/triangle/.eslintrc
@@ -0,0 +1,14 @@
+{
+  "root": true,
+  "extends": "@exercism/eslint-config-javascript",
+  "env": {
+    "jest": true
+  },
+  "overrides": [
+    {
+      "files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
+      "excludedFiles": ["custom.spec.js"],
+      "extends": "@exercism/eslint-config-javascript/maintainers"
+    }
+  ]
+}
diff --git a/javascript/triangle/.npmrc b/javascript/triangle/.npmrc
new file mode 100644
index 0000000..d26df80
--- /dev/null
+++ b/javascript/triangle/.npmrc
@@ -0,0 +1 @@
+audit=false
diff --git a/javascript/triangle/HELP.md b/javascript/triangle/HELP.md
new file mode 100644
index 0000000..e4a2c05
--- /dev/null
+++ b/javascript/triangle/HELP.md
@@ -0,0 +1,73 @@
+# Help
+
+## Running the tests
+
+## Setup
+
+Go through the setup [instructions for JavaScript][docs-exercism-javascript] to install the necessary dependencies.
+
+## Requirements
+
+Install assignment dependencies:
+
+```shell
+# Using npm
+npm install
+
+# Alternatively using yarn
+yarn
+```
+
+## Making the test suite pass
+
+All exercises come with a test suite to help you validate your solution before submitting.
+You can execute these tests by opening a command prompt in the exercise's directory, and then running:
+
+```bash
+# Using npm
+npm test
+
+# Alternatively using yarn
+yarn test
+```
+
+In some 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`.
+
+## Writing custom tests
+
+If you wish to write additional, custom, tests, create a new file `custom.spec.js`, and submit it with your solution together with the new file:
+
+```shell
+exercism submit numbers.js custom.spec.js
+```
+
+[docs-exercism-javascript]: https://exercism.org/docs/tracks/javascript/installation
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit triangle.js` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [JavaScript track's documentation](https://exercism.org/docs/tracks/javascript)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+- [Gitter](https://gitter.im/exercism/support) is Exercism's Gitter room; go here to get support and ask questions if you face any issues with downloading or submitting your exercises.
+- [/r/javascript](https://www.reddit.com/r/javascript) is the Javascript subreddit.
+- [StackOverflow](https://stackoverflow.com/questions/tagged/javascript+exercism) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
+- [Github issue tracker](https://github.com/exercism/javascript/issues) is where we track our development and maintainance of Javascript exercises in exercism. But if none of the above links help you, feel free to post an issue here.
\ No newline at end of file
diff --git a/javascript/triangle/LICENSE b/javascript/triangle/LICENSE
new file mode 100644
index 0000000..90e73be
--- /dev/null
+++ b/javascript/triangle/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2021 Exercism
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/javascript/triangle/README.md b/javascript/triangle/README.md
new file mode 100644
index 0000000..5cdfc26
--- /dev/null
+++ b/javascript/triangle/README.md
@@ -0,0 +1,49 @@
+# Triangle
+
+Welcome to Triangle on Exercism's JavaScript Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+Determine if a triangle is equilateral, isosceles, or scalene.
+
+An _equilateral_ triangle has all three sides the same length.
+
+An _isosceles_ triangle has at least two sides the same length. (It is sometimes
+specified as having exactly two sides the same length, but for the purposes of
+this exercise we'll say at least two.)
+
+A _scalene_ triangle has all sides of different lengths.
+
+## Note
+
+For a shape to be a triangle at all, all sides have to be of length > 0, and
+the sum of the lengths of any two sides must be greater than or equal to the
+length of the third side. See [Triangle Inequality](https://en.wikipedia.org/wiki/Triangle_inequality).
+
+## Dig Deeper
+
+The case where the sum of the lengths of two sides _equals_ that of the
+third is known as a _degenerate_ triangle - it has zero area and looks like
+a single line. Feel free to add your own code/tests to check for degenerate triangles.
+
+## Source
+
+### Created by
+
+- @rchavarria
+
+### Contributed to by
+
+- @ankorGH
+- @matthewmorgan
+- @msomji
+- @ovidiu141
+- @ryanplusplus
+- @SleeplessByte
+- @tejasbubane
+- @WebCu
+
+### Based on
+
+The Ruby Koans triangle project, parts 1 & 2 - http://rubykoans.com
\ No newline at end of file
diff --git a/javascript/triangle/babel.config.js b/javascript/triangle/babel.config.js
new file mode 100644
index 0000000..5f1ec60
--- /dev/null
+++ b/javascript/triangle/babel.config.js
@@ -0,0 +1,15 @@
+module.exports = {
+  presets: [
+    [
+      '@babel/preset-env',
+      {
+        targets: {
+          node: 'current',
+        },
+        useBuiltIns: 'entry',
+        corejs: '3.17',
+      },
+    ],
+  ],
+  plugins: ['@babel/plugin-syntax-bigint'],
+};
diff --git a/javascript/triangle/package.json b/javascript/triangle/package.json
new file mode 100644
index 0000000..e0d83eb
--- /dev/null
+++ b/javascript/triangle/package.json
@@ -0,0 +1,31 @@
+{
+  "name": "@exercism/javascript-triangle",
+  "description": "Exercism exercises in Javascript.",
+  "author": "Katrina Owen",
+  "private": true,
+  "license": "MIT",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/exercism/javascript",
+    "directory": "exercises/practice/triangle"
+  },
+  "devDependencies": {
+    "@babel/cli": "^7.15.4",
+    "@babel/core": "^7.15.5",
+    "@babel/plugin-syntax-bigint": "^7.8.3",
+    "@babel/preset-env": "^7.15.4",
+    "@exercism/eslint-config-javascript": "^0.5.0",
+    "@types/jest": "^26.0.24",
+    "@types/node": "^16.7.10",
+    "babel-jest": "^26.6.3",
+    "core-js": "^3.17.2",
+    "eslint": "^7.32.0",
+    "jest": "^26.6.3"
+  },
+  "dependencies": {},
+  "scripts": {
+    "test": "jest --no-cache ./*",
+    "watch": "jest --no-cache --watch ./*",
+    "lint": "eslint ."
+  }
+}
diff --git a/javascript/triangle/triangle.js b/javascript/triangle/triangle.js
new file mode 100644
index 0000000..dba1449
--- /dev/null
+++ b/javascript/triangle/triangle.js
@@ -0,0 +1,43 @@
+'use strict';
+
+export class Triangle {
+    constructor(s1, s2, s3) {
+        this.s1 = s1;
+        this.s2 = s2;
+        this.s3 = s3;
+    }
+
+    get isEquilateral() {
+        return (this.isTriangle()
+                && this.s1 === this.s2
+                && this.s2 === this.s3);
+    }
+
+    get isIsosceles() {
+        return (this.isTriangle()
+                && (
+                    this.s1 === this.s2
+                        || this.s2 === this.s3
+                        || this.s3 === this.s1
+                ));
+    }
+
+    get isScalene() {
+        return (this.isTriangle()
+                && this.s1 !== this.s2
+                && this.s2 !== this.s3
+                && this.s3 !== this.s1);
+    }
+
+    isTriangle() {
+        if (
+            // Triangle inequality.
+            !(this.s1 + this.s2 > this.s3
+             && this.s1 + this.s3 > this.s2
+             && this.s2 + this.s3 > this.s1)
+            // Sides of a triangle cannot be zero.
+                || (this.s1 === 0 || this.s2 === 0 || this.s3 === 0)
+        ) return false;
+        return true;
+    }
+}
diff --git a/javascript/triangle/triangle.spec.js b/javascript/triangle/triangle.spec.js
new file mode 100644
index 0000000..afa2aef
--- /dev/null
+++ b/javascript/triangle/triangle.spec.js
@@ -0,0 +1,104 @@
+import { Triangle } from './triangle';
+
+describe('Triangle', () => {
+  describe('equilateral triangle', () => {
+    test('all sides are equal', () => {
+      const triangle = new Triangle(2, 2, 2);
+      expect(triangle.isEquilateral).toBe(true);
+    });
+
+    test('any side is unequal', () => {
+      const triangle = new Triangle(2, 3, 2);
+      expect(triangle.isEquilateral).toBe(false);
+    });
+
+    test('no sides are equal', () => {
+      const triangle = new Triangle(5, 4, 6);
+      expect(triangle.isEquilateral).toBe(false);
+    });
+
+    test('all zero sides is not a triangle', () => {
+      const triangle = new Triangle(0, 0, 0);
+      expect(triangle.isEquilateral).toBe(false);
+    });
+
+    test('sides may be floats', () => {
+      const triangle = new Triangle(0.5, 0.5, 0.5);
+      expect(triangle.isEquilateral).toBe(true);
+    });
+  });
+
+  describe('isosceles triangle', () => {
+    test('last two sides are equal', () => {
+      const triangle = new Triangle(3, 4, 4);
+      expect(triangle.isIsosceles).toBe(true);
+    });
+
+    test('first two sides are equal', () => {
+      const triangle = new Triangle(4, 4, 3);
+      expect(triangle.isIsosceles).toBe(true);
+    });
+
+    test('first and last sides are equal', () => {
+      const triangle = new Triangle(4, 3, 4);
+      expect(triangle.isIsosceles).toBe(true);
+    });
+
+    test('equilateral triangles are also isosceles', () => {
+      const triangle = new Triangle(4, 4, 4);
+      expect(triangle.isIsosceles).toBe(true);
+    });
+
+    test('no sides are equal', () => {
+      const triangle = new Triangle(2, 3, 4);
+      expect(triangle.isIsosceles).toBe(false);
+    });
+
+    test('first triangle inequality violation', () => {
+      const triangle = new Triangle(1, 1, 3);
+      expect(triangle.isIsosceles).toBe(false);
+    });
+
+    test('second triangle inequality violation', () => {
+      const triangle = new Triangle(1, 3, 1);
+      expect(triangle.isIsosceles).toBe(false);
+    });
+
+    test('third triangle inequality violation', () => {
+      const triangle = new Triangle(3, 1, 1);
+      expect(triangle.isIsosceles).toBe(false);
+    });
+
+    test('sides may be floats', () => {
+      const triangle = new Triangle(0.5, 0.4, 0.5);
+      expect(triangle.isIsosceles).toBe(true);
+    });
+  });
+
+  describe('scalene triangle', () => {
+    test('no sides are equal', () => {
+      const triangle = new Triangle(5, 4, 6);
+      expect(triangle.isScalene).toBe(true);
+    });
+
+    test('all sides are equal', () => {
+      const triangle = new Triangle(4, 4, 4);
+      expect(triangle.isScalene).toBe(false);
+    });
+
+    test('two sides are equal', () => {
+      const triangle = new Triangle(4, 4, 3);
+      expect(triangle.isScalene).toBe(false);
+    });
+
+    test('may not violate triangle inequality', () => {
+      const triangle = new Triangle(7, 3, 2);
+      expect(triangle.isScalene).toBe(false);
+    });
+
+    test('sides may be floats', () => {
+      const triangle = new Triangle(0.5, 0.4, 0.6);
+      expect(triangle.isScalene).toBe(true);
+    });
+  });
+});