You are viewing a single comment's thread. Return to all comments →
You should call the constructor of the parent class from the child class, i.e., the Square class constructor should call super(s, s).
super(s, s)
You can check the editorial.
I noticed utilizing super() and super(s) can pass the test case, too. Why is that? Thank you.
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; } };
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Day 5: Inheritance
You are viewing a single comment's thread. Return to all comments →
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.
I noticed utilizing super() and super(s) can pass the test case, too. Why is that? Thank you.
super() initializes its parent constructor. In this case it calls constructor of Rectangle. So using super(s,s) will initialize w and h.
However if you do not provide proper argument to super then you have to manually assign the value of w and h. Like: