Test your skills: Object-oriented JavaScript
The aim of this skill test is to help you assess whether you've understood our Classes in JavaScript article.
Note: To get help, read our Test your skills usage guide. You can also reach out to us using 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.
To complete the task:
- 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.
js
class Shape {
name;
sides;
sideLength;
}
Click here to show the solution
Your finished JS could look something like this:
js
class Shape {
name;
sides;
sideLength;
constructor(name, sides, sideLength) {
this.name = name;
this.sides = sides;
this.sideLength = sideLength;
}
calcPerimeter() {
console.log(
`The ${this.name}'s perimeter length is ${this.sides * this.sideLength}.`,
);
}
}
const square = new Shape("square", 4, 5);
square.calcPerimeter();
const triangle = new Shape("triangle", 3, 3);
triangle.calcPerimeter();
OOJS 2
Now it's time to add some inheritance into the mix.
To complete the task:
- Create a
Square
class that inherits fromShape
. - Add a
calcArea()
method toSquare
that calculates its area. - Set up the
Square
constructor so that thename
property ofSquare
object instances is automatically set tosquare
, and thesides
property is automatically set to4
. When invoking the constructor, you should therefore just need to provide thesideLength
property. - Create an instance of the
Square
class calledsquare
with appropriate property values, and call itscalcPerimeter()
andcalcArea()
methods to show that it works OK.
js
class Shape {
name;
sides;
sideLength;
constructor(name, sides, sideLength) {
this.name = name;
this.sides = sides;
this.sideLength = sideLength;
}
calcPerimeter() {
console.log(
`The ${this.name}'s perimeter length is ${this.sides * this.sideLength}.`,
);
}
}
// Don't edit the code above here!
// Add your code here
Click here to show the solution
Your finished JS could look something like this:
js
// ...
// Don't edit the code above here!
class Square extends Shape {
constructor(sideLength) {
super("square", 4, sideLength);
}
calcArea() {
console.log(
`The ${this.name}'s area is ${this.sideLength * this.sideLength} squared.`,
);
}
}
const square = new Square(4);
square.calcPerimeter();
square.calcArea();