Write a JavaScript program to create a class called ‘Rectangle’ with properties for width and height. Include two methods to calculate rectangle area and perimeter. Create an instance of the ‘Rectangle’ class and calculate its area and perimeter.
In the above exercise we create a “Person” class with properties for name, age, and country. It includes a method to display the person’s details.
Finally it creates two instances of the Person class and displays their details using the displayDetails() method.
Solution:
classRectangle{constructor(width,height){this.width=width;this.height=height;}calculateArea(){returnthis.width*this.height;}calculatePerimeter(){return2*(this.width+this.height);}}// Create an instance of the Rectangle class
constrectangle=newRectangle(12,10);// Calculate area and perimeter of the rectangle
constarea=rectangle.calculateArea();constperimeter=rectangle.calculatePerimeter();// Display the results
console.log(`Rectangle Area: ${area}`);console.log(`Rectangle Perimeter: ${perimeter}`);