I am to implement a polygon class with a constructor that takes an array of integer side lengths, and a perimeter method that returns the sum of the polygon’s side lengths.
This is the input:
const rectangle = new Polygon([10, 20, 10, 20]);
const square = new Polygon([10, 10, 10, 10]);
const pentagon = new Polygon([10, 20, 30, 40, 43]);
And this is what I wrote:
class Polygon {
constructor(lengths){
this.length1 = lengths[0];
this.length2 = lengths[1];
this.length3 = lengths[2];
this.length4 = lengths[3];
this.length5 = lengths[4];
}
perimeter () {
return this.length1 + this.length2 + this.length3 + this.length4 + this.length5;
}
}