• + 1 comment

    what's wrong with my code??

    /*
     *  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(s){
                this.s = s;
            }
            area() {
                return (this.s*this.s);
            }
        }
    
    • HackerRank AdminChallenge Author
      + 1 comment

      You should call the constructor of the parent class from the child class, i.e., the Square class constructor should call super(s, s).

      You can check the editorial.

      • + 1 comment

        I noticed utilizing super() and super(s) can pass the test case, too. Why is that? Thank you.

        • + 0 comments

          super() initializes its parent constructor. In this case it calls constructor of Rectangle. So using super(s,s) will initialize w and h.

          Rectangle.prototype.area = function() {
              return(this.w*this.h);
          };
          
          class Square extends Rectangle {
              constructor(s) {
                  super(s,s);
              }
          };
          

          However if you do not provide proper argument to super then you have to manually assign the value of w and h. Like:

          class Square extends Rectangle {
              constructor(s) {
                  super()
                  this.w = s;
                  this.h = s;
              }
          };