We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Classes
You are viewing a single comment's thread. Return to all 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?