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, andsideLengthproperties, 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
Shapeclass calledsquare. Give it anameofsquare,4sides, and asideLengthof5. - 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
Shapecalledtriangle, with anameoftriangle,3sidesand asideLengthof3. - 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
Squareclass that inherits fromShape. - Add a
calcArea()method toSquarethat calculates its area. - Set up the
Squareconstructor so that thenameproperty ofSquareobject instances is automatically set tosquare, and thesidesproperty is automatically set to4. When invoking the constructor, you should therefore just need to provide thesideLengthproperty. - Create an instance of the
Squareclass calledsquarewith 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();