Created
March 4, 2021 15:03
-
-
Save MinSomai/1e6e85862e999ffea1a2385830133f30 to your computer and use it in GitHub Desktop.
Day 4 : Javascript | Classes - Hackerrank.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(polygonSideLengths){ | |
this.polygonSideLengths = polygonSideLengths | |
} | |
perimeter(){ | |
return this.polygonSideLengths.reduce((acc, item)=>acc+=item) | |
} | |
} | |
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment