Giter Club home page Giter Club logo

trianglechallenge's Introduction

Triangle challenge

Description: Write a program that will determine the type of a triangle. It should take the lengths of the triangle's three sides as input, and return whether the triangle is equilateral, isosceles or scalene.

How to run

  • git clone https://github.com/katkelemen/triangleChallenge.git
  • npm install
  • to run the tests: npm test

My steps of implementing

  • I set up a testing environment using Jasmine from https://jasmine.github.io/setup/nodejs.html

  • I wrote the first 3 tests and made them pass. This also checks if my environment is set up and running correctly. The basic idea is that if a triangle's all 3 sides are the same size, then it is an equilateral triangle, if 2 sides are the same size, then it is an isosceles triangle, otherwise its a scalene triangle.

  • After this I started thinking of error cases, created the tests for them and made them pass. The function should raise an error if:

    • there is less than 3 arguments provided
    • at least one of the arguments are 0 or negative
    • at least one of the arguments are not numbers
    • it should work with floating point numbers
  • I wanted to be sure I didn't forget something so I checked if there is any other requirements for the 3 sides to form a triangle and realised I need to make a case for the inequality rule, which states that the length of a side of a triangle is less than the sum of the lengths of the other two sides and greater than the difference of the lengths of the other two sides.

  • I had this function at that point:

function triangleType (sideA, sideB, sideC) {
	var positiveNumbers = sideA > 0 && sideB > 0 && sideC > 0;
	var validTriangle = sideA + sideB > sideC && sideB + sideC > sideA && sideA + sideC > sideB;
	if (positiveNumbers && validTriangle) {
		if (sideA === sideB && sideB === sideC) {
			return 'equilateral'
		} else if (sideA === sideB || sideB === sideC || sideC === sideA) {
			return 'isosceles'
		} else {
			return 'scalene'
		}
	}
	return null;
};

The problem here is that this returns with a null for all error cases so I rewrote it to throw errors that will tell the function user more explicitly about what went wrong.

I also used a linter to make sure my code is good quality and safe.

trianglechallenge's People

Contributors

katkelemen avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.