JavaScript · Exercise 5 of 5 Previous
Advanced
A shape gallery with inheritance
Shape is the base class. Its area() throws, because each kind of shape must provide its own; describe() uses area().
Write Circle extends Shape (radius), Rectangle extends Shape (width, height) and Square extends Rectangle (one side). Each must call super(...), and describe() must start with the right name: "Circle", "Rectangle" or "Square".
Render the three shapes new Circle(1), new Rectangle(2, 3) and new Square(2) into #gallery as <li> items showing describe(), sorted by area, smallest first.
What Check tests
new Circle(1).describe()isCircle with area 3.14new Rectangle(2, 3).describe()isRectangle with area 6.00- A Square is a Rectangle and a Shape, named
Square - A plain Shape still refuses to give an area
- The gallery shows the three shapes, smallest area first
- Subclasses use
extendsandsuper - No errors
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Your page
Press Run to see the page.
Console
Nothing logged yet.