Test your skills: Object-oriented JavaScript - Learn web development | MDN 25.02.
26, 01:06
Advanced JavaScript objects Test: OOJS tests index Test: Object-oriented JavaScript Eng?ish (US)
Test your ski,,s: Object-oriented
JavaScript
Previous Overview: Advanced JavaScript objects Next
The aim of this ski?? test is to he?p you assess whether you've understood our C?asses in JavaScript artic?e.
Note: To get he?p, read our Test your ski??s usage guide. You can a?so reach out to us using one of our
communication channe?s.
OOJS 1
En this task we provide you with the start of a definition for a Shape c?ass. Et has three properties: name , sides ,
and sideLength . This c?ass on?y mode?s shapes for which a?? sides are the same ?ength, ?ike a square or an
equi?atera? triang?e.
To comp?ete the task:
IJK Add a constructor to this c?ass. The constructor takes arguments for the name , sides , and sideLength
properties, and initia?izes them.
MJK Add a new method calcPerimeter() method to the c?ass, which ca?cu?ates its perimeter (the ?ength of the
shape's outer edge) and ?ogs the resu?t to the conso?e.
PJK Create a new instance of the Shape c?ass ca??ed square . Give it a name of square , 4 sides , and a
sideLength of 5 .
RJK Ca?? your calcPerimeter() method on the instance, to see whether it ?ogs the ca?cu?ation resu?t to the
browser's conso?e as expected.
SJK Create a new instance of Shape ca??ed triangle , with a name of triangle , 3 sides and a sideLength
of 3 .
TJK Ca?? [Link]() to check that it works OK.
JS
[Link] Page 1 of 3
Test your skills: Object-oriented JavaScript - Learn web development | MDN 25.02.26, 01:06
class Shape {
name;
sides;
sideLength;
}
▸KC?ick here to show the so?ution
OOJS 2
Now it's time to add some inheritance into the mix.
To comp?ete the task:
IJK Create a Square c?ass that inherits from Shape .
MJK Add a calcArea() method to Square that ca?cu?ates its area.
PJK Set up the Square constructor so that the name property of Square object instances is automatica??y set to
square , and the sides property is automatica??y set to 4 . When invoking the constructor, you shou?d
therefore just need to provide the sideLength property.
RJK Create an instance of the Square c?ass ca??ed square with appropriate property va?ues, and ca?? its
calcPerimeter() and calcArea() methods to show that it works OK.
JS
[Link] Page 2 of 3
Test your skills: Object-oriented JavaScript - Learn web development | MDN 25.02.26, 01:06
class Shape {
name;
sides;
sideLength;
constructor(name, sides, sideLength) {
[Link] = name;
[Link] = sides;
[Link] = sideLength;
}
calcPerimeter() {
[Link](
`The ${[Link]}'s perimeter length is ${[Link] * [Link]}.`,
);
}
}
// Don't edit the code above here!
// Add your code here
▸KC?ick here to show the so?ution
Previous Overview: Advanced JavaScript objects Next
Your b?ueprint for a better internet.
[Link] Page 3 of 3