Polygon class help

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 {
        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; 

So what happens when only 4 lengths are passed into the constructor? What is the value of this.length5? How will that affect the return value?

Yeah I thought of that too but there is no compiler error. It still runs but outputs “NaN” for 4 lengths or less.

JS will happily add things together that really shouldn’t be added together, so there is no compiler error. It is up to you to make sure that they don’t get added together in the first place. So I’ll ask again, if there are only four lengths passed into the constructor then what is the value of this.length5 in the perimeter method? And when you add that value to a number what do you get? And how can you fix perimeter so that it doesn’t add values that shouldn’t be added together?

I’m new so I’m thinking either null or 0?

My first thought was a for loop:

for (i=0; i<array.length; i++){
            sum += lengths[i]; 

But I ran into a problem. The scope of the perimeter method does not recognize the array passed into the constructor. Which I don’t understand because it’s in the same class.

I will give you a hint, it is neither of those :slight_smile: Do you know what value a variable has by default if you don’t explicitly assign it a value:

var a;

What is the value of a after that statement executes? This is one of the most basic concepts of JS, so you definitely need to know this. Are you taking a course of some kind? If so, I would find it hard to believe that you would be working on classes without having been taught this.

Yes, a for loop would work here. The constructor is being passed an array. So don’t you think you could just save that array as a local field for the class? I would highly recommend you actually save a copy of the array. So in the constructor, instead of saving each length in its own variable you would just save the array (let’s call it lengths):

  this.lengths = ???;

Your job is to figure out how to save a copy of the array being passed into the constructor (i.e. fill in the ??? with a method for creating a copy of lengths). If you aren’t sure how to do this then I would use the googles to search for something like “javascript make copy of array”.

Then after you have this.lengths set you can use a for loop to go through it and add all the lengths.


I’m doing 10 days of js on hacker rank and they’re just diving right in.
When a parameter is passed in I’m used to it being on the right side of the assignment operator. It’s strange seeing it on the left. I searched around but can’t seem to find it. Something like this?

this.lengths = []

Correct! Now what is undefined + 10? That is why you were getting NaN when only four numbers were in the array.

this.lengths is a completely different variable than the lengths array being passed into the constructor. If it is confusing, then you can call it something else, like:

this.arrOfLengths = ???;

Now you’ll just need to do something to the lengths array being passed into the constructor in order to make a copy of it.

const cloneLengths = [...lengths];
this.arrOfLengths = cloneLengths; ?

Yes, that is one way to copy/clone an array. If you don’t understand what the ... is doing then I suggest you read up on the JS spread operator.

Also, you can forgo the middleman here and just do:

this.arrOfLengths = [...lengths];

Now your class contains an array of the lengths that were passed into the constructor and you can loop through them to get a perimeter. There is an alternative way to sum those numbers, which I think the majority of JS programmers would use. If you are interested, check out the Array.reduce method.

I tried this but it says “arrOfLengths is not defined” in the for loop line.

class Polygon {
        this.arrOfLengths = [...lengths];
    perimeter () {
        let sum = 0; 
        for (let i of arrOfLengths){
            sum += i;
        return sum;

Remember earlier when I pointed out that lengths and this.lengths are not the same thing? Well, same applies here. When inside the class, you have to use this to refer to properties of the class.

1 Like