The description of the problem is given in comment at the top of the code. The output I get is NaN instead of an integer value of perimeter
/*
* Implement a Polygon class with the following properties:
* 1. A constructor that takes an array of integer side lengths.
* 2. A 'perimeter' method that returns the sum of the Polygon's side lengths.
*/
class Polygon
{
constructor(sides)
{
this.sides = sides;
}
perimeter()
{
var per = 0;
for (var i = 0; i <= this.sides.length; i++)
{
per += this.sides[i];
}
return per;
}
}
This piece of code runs the above code:
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]);
console.log(rectangle.perimeter());
console.log(square.perimeter());
console.log(pentagon.perimeter());