Command Palette

Search for a command to run...

Hectal
Phase 7: this, Prototypes and Classes
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

  1. new Circle(1).describe() is Circle with area 3.14
  2. new Rectangle(2, 3).describe() is Rectangle with area 6.00
  3. A Square is a Rectangle and a Shape, named Square
  4. A plain Shape still refuses to give an area
  5. The gallery shows the three shapes, smallest area first
  6. Subclasses use extends and super
  7. 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.