JS10 Day 4: Classes

개요[ | ]

JS10 Day 4: Classes
해커랭크 10 Days of Javascript
# 문제 비고
4-5 Day e
12 JS10 Day 4: Create a Rectangle Object
13 JS10 Day 4: Count Objects
14 JS10 Day 4: Classes
15 JS10 Day 5: Inheritance
16 JS10 Day 5: Template Literals
17 JS10 Day 5: Arrow Functions

/*
 * 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.
 */
function Polygon(lengths) {
    this.lengths = lengths;
    this.perimeter = function() {
        return this.lengths.reduce((a,b)=>a+b);
    }
}
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());
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}