Test your skills: Object-oriented JavaScript
The aim of this skill test is to assess whether you've understood our Classes in JavaScript article.
Note: You can try solutions in the interactive editors on this page or in an online editor such as CodePen, JSFiddle, or Glitch. If there is an error in your code, it will be logged into the results panel on this page or in the JavaScript console.
If you get stuck, you can reach out to us in one of our communication channels.
OOJS 1
In this task we provide you with the start of a definition for a Shape
class. It has three properties: name
, sides
, and sideLength
. This class only models shapes for which all sides are the same length, like a square or an equilateral triangle.
We'd like you to:
- Add a constructor to this class. The constructor takes arguments for the
name
,sides
, andsideLength
properties, and initializes them. - Add a new method
calcPerimeter()
method to the class, which calculates its perimeter (the length of the shape's outer edge) and logs the result to the console. - Create a new instance of the
Shape
class calledsquare
. Give it aname
ofsquare
,4
sides
, and asideLength
of5
. - Call your
calcPerimeter()
method on the instance, to see whether it logs the calculation result to the browser's console as expected. - Create a new instance of
Shape
calledtriangle
, with aname
oftriangle
,3
sides
and asideLength
of3
. - Call
triangle.calcPerimeter()
to check that it works OK.
Try updating the live code below to recreate the finished example:
Download the starting point for this task to work in your own editor or in an online editor.
OOJS 2
Next we'd like you to create a Square
class that inherits from Shape
, and adds a calcArea()
method that calculates the square's area. Also set up the constructor so that the name
property of Square
object instances is automatically set to square
, and the sides
property is automatically set to 4
. When invoking the constructor, you should therefore just need to provide the sideLength
property.
Create an instance of the Square
class called square
with appropriate property values, and call its calcPerimeter()
and calcArea()
methods to show that it works OK.
Try updating the live code below to recreate the finished example:
Download the starting point for this task to work in your own editor or in an online editor.