Coder Social home page Coder Social logo

ts's Introduction

Tradeshift Triangle Test

Instructions

  • To run: npm start
  • To test: npm test

Make sure to install dependencies through npm first.

Considerations & Design Decisions

The first decision I made, was to use a simple function which takes 3 arguments (length of each side of the triangle) which is placed in a separate file.

I had the consideration to create a Triangle class, which could have expanded functionality. As I believe in the simplest solution being the best, I didn't want to speculate in expanded functionality and what might become potential use cases further along.

If I knew that there would be other operations regarding triangles, I would probably consider a more scalable solution, as there would be a bit of lock-in to using functions rather than class methods with this approach. But that's pure speculation at this point.

Afterwards it was straight forward to write the tests required for the functionality to be covered. An error is thrown if the input is not valid.

I am also a fan of using constants as I think this scales well and mitigates the risk of error. It may look clunky, but helps peer developers read and extend your code. Each triangle type is hence defined as a constant.

Other than the constants, the single function triangleType is exported from geometry.js. triangleType either throws an error or returns one of the constant values.

You also find a helper function called isFiniteAndPositive which is used to validate the arguments, i.e. side length. It validates that the side lengths provided are finite numbers and positive, thus errors on e.g. strings, negative numbers or zero. It could also have been an anonymous function, but I personally like the explicite nature of named helpers functions for readability.

I considered adding a parseFloat call before validating the input to allow to numeric strings to be passed, but I honestly thought that would be odd to put in the function itself. A use case for this could come from a user providing input in a form, where input is always read as strings. But in this case it would be a better design to parse the string before passing it to triangleType.

Another thing I considered was to unpack the arguments into an array, i.e. this function definition: (...sides) => { [...] }. While it would remove the need to create an array to run isFiniteAndPositive through call to every, it would also require adding 1 more argument check: that there are three arguments provided to the function. I think this solution is more readable.

ts's People

Contributors

egeriis avatar

Watchers

 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.