Exercise 2: Finding the Type of Triangle
In this exercise, you will implement class-based inheritance using the two constructor function Shape and Triangle.
We'll cover the following
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
andsides
toShape
and initialize them. -
Define the function
displayName
on the prototype ofShape
. The function should return thename
property. -
Next, you need to initialize the
Triangle
constructor function such that it gets all theShape
properties initialized for it and also takes in and initializes the additional properties:a
,b
andc
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 theShape
class.
Task 2
-
You also need to define the
triangleType
function on the prototype ofTriangle
. The function should compare the sidesa
,b
andc
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