Sort by

recency

|

284 Discussions

|

  • + 0 comments
    class Polygon {
        constructor(sideLenghts){
            this.sides = sideLenghts;
        }
        
        perimeter(){
            let sum = 0;
            for (let side of this.sides) {
                sum += side;
            }
            return sum;
        }
    }
    
  • + 0 comments
    class Polygon{
        constructor(arr){
            this.arr = arr;
        }
        perimeter(){
            let sum=0;
            for(let i=0; i<this.arr.length; i++){
                    sum+=this.arr[i];
            }
            return sum;
        }
    }
    
  • + 0 comments

    class Polygon { constructor(arr){ this.arr = arr } perimeter(){ return this.arr.reduce((sum, current) => sum + current, 0) } }

  • + 0 comments

    class Polygon { constructor(sides) { this.sides = sides; } sum = 0; perimeter(){ for(let i = 0; i < this.sides.length; i++) { this.sum = this.sum + this.sides[i]; } return this.sum; } } const poly1 = new Polygon([10, 20, 30]); console.log(poly1.perimeter());

    const poly2 = new Polygon([10, 10, 10, 10]); console.log(poly2.perimeter());

    const poly3 = new Polygon([50, 40, 30, 20, 3]); console.log(poly3.perimeter());

    This code satisfies the condition and produce the expected result in my IDE. However when I try to run the testcases, it prints 2 times. Anybody know why it's happening?

  • + 0 comments

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

    Polygon.prototype.perimeter = function() { let sum=0; this.sides.forEach(side=> sum += side); return sum; }