Exercise 2: Finding the Type of Triangle

In this exercise, you will implement class-based inheritance using the two constructor function Shape and Triangle.

Problem Statement

In this exercise, two constructor functions, Shape and Triangle are declared. You need to implement class-based inheritance such that the class Triangle inherits prototype properties from the Shape class.

You have to implement the following tasks:

Task 1

  • Pass the parameters name and sides to Shape and initialize them.

  • Define the function displayName on the prototype of Shape. The function should return the name property.

  • Next, you need to initialize the Triangle constructor function such that it gets all the Shape properties initialized for it and also takes in and initializes the additional properties: a, b and c that denote the sides of a triangle.

  • After that you need to implement class-based inheritance such that Triangle inherits the prototype properties/methods from the Shape class.

Task 2

  • You also need to define the triangleType function on the prototype of Triangle. The function should compare the sides a, b and c to return:

  • Equilateral if all three sides are equal.

  • Isosceles if two sides are equal.

  • Scalene if none of the sides are equal.

Note: Please use the same spellings as mentioned above for everything else the test cases might not pass.

Sample Input

The following functions will be tested:

displayName()
triangleType(3,5,4)
triangleType(3,3,4)
triangleType(3,3,3)

Sample Output

Triangle
Scalene
Isosceles
Equilateral

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy