Sunday, 27 December 2015

Kata Day 32: Length of the line segment


Problem Description: Find the length between 2 co-ordinates. The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array:

(0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ]

The function must return the answer rounded to 2 decimal places in the form of a string.

lengthOfLine([ [ 0 , 0 ] , [ 5, -7 ] ]); => "8.60"
If the 2 given co-ordinates are the same, the returned length should be "0.00"

Level:7kyu

Link to Kata: Length of the line segment

Our old school distance theorem in form of a Kata. We have to find distance between two points which are given as two arrays. I initialized the four coordinates as x1,y1,x2,y2 from the input arrays and then applied the distance formula by using the Math functions of Javascript. I then converted the value which I got after applying distance formula to a string and parsed it to 2 decimal places.

In the end I returned the parsed value as the solution to this Kata.

Here is a link to the code: Kata Day 32

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment