In this HackerRank Day 5: Inheritance 10 days of javascript problem solution We provide the implementation for a Rectangle class in the editor. Perform the following tasks:
  1. Add an area method to Rectangle's prototype.
  2. Create a Square class that satisfies the following:
  3. It is a subclass of Rectangle.
  4. It contains a constructor and no other methods.
  5. It can use the Rectangle class' area method to print the area of a square object.
  6. Locked code in the editor tests the class and method implementations and prints the area values to STDOUT.


HackerRank Day 5: Inheritance problem solution


Problem solution in JavaScript programming.

/*
 *  Write code that adds an 'area' method to the Rectangle class' prototype
 */
Rectangle.prototype.area = function(){
    return (this.w * this.h);
}
/*
 * Create a Square class that inherits from Rectangle and implement its class constructor
 */
class Square extends Rectangle {
    constructor(r){
        super(r,r);
    }
}