1

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());
Bhushan Oza
  • 112
  • 1
  • 10
  • 4
    You're iterating too far: `i <= this.sides.length` should be `i < this.sides.length`. I vote to close as a typo. – trincot Mar 18 '19 at 14:58
  • Change `<=` to `<` in your loop :) – Sergej Mar 18 '19 at 14:58
  • indexes in arrays start from 0, and goes until N - 1. Thus, as others stated you should replace `<=` with `<` – Onur Arı Mar 18 '19 at 14:59
  • 1
    Is also duplicate of [this](https://stackoverflow.com/questions/41729085/how-to-sum-numbers-saved-as-array-in-javascript-with-while-loop) together with [this](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers) – trincot Mar 18 '19 at 15:05

3 Answers3

2

Change <= for < so you don't go over the bounds of your sides array:

for (var i = 0; i <= this.sides.length; i++)

for:

for (var i = 0; i < this.sides.length; i++)

You can use reduce to sum your sides:

class Polygon {
  constructor(sides) {
      this.sides = sides;
  }

  perimeter() {
    return this.sides.reduce((sum, side) => sum + side, 0);
  }
}

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());
jo_va
  • 13,504
  • 3
  • 23
  • 47
0

The limit of your for loop goes 1 past the end of the array.

for (var i = 0; i <= this.sides.length; i++)

The array of size length will have length - 1 as the last index. To fix this, use:

for (var i = 0; i < this.sides.length; i++)

Kon
  • 4,023
  • 4
  • 24
  • 38
0

The other answers are correct, the problem is that the i will go one off the end of the array, and that'll give you problems. This is an illustration of a problem which can arise from using for loops to iterate over an array; i is only incidentally related to the array over which you're iterating, and it's easy enough to have problems where the relationship that we think we're creating between i and the array is not quite the same as that which we create in the code.

For this reason, I'd recommend using something like reduce. You might also use forEach and keep track of the sum, but reduce is by far the best option for a sum as it's designed to take something iterable and produce a single value.

Here's how that might look:

class Polygon
{
    constructor(sides)
    {
        this.sides = sides;
    }

    perimeter()
    {
      return this.sides.reduce((prev, curr) => prev + curr, 0);
    }
}

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());
OliverRadini
  • 6,238
  • 1
  • 21
  • 46