관리 메뉴

Silver Library (Archived)

알고리즘 풀이 - Day 14 본문

Face the fear, build the future

알고리즘 풀이 - Day 14

Chesed Kim 2021. 7. 14. 17:23
반응형

'진짜 이래도 되는걸까?' 라는 생각이 아직은 계속 들지만, 역시 이 방법으로 시작하는 게 맞는 것 같다는 생각이 듭니다.

적어도 지금은 말이죠. 생각해보면 다른 언어도 처음엔 이렇게 배웠던게 아닌가 싶습니다.

 

자료 구조는 배우면 배울수록 '코딩 테스트 의도는 둘째치고, 자료 구조 기술 면접에 대한 내용은 정말 심정이 이해가 간다' 였습니다.

 

알고리즘 풀이는 솔직히 말하건데, 그냥 최대한 한번 생각나는 대로 입력 해 보고 막히면 답 찾아서 보고 있습니다.

그리고 다시 따라해보며 입력을 해보며 '저리 표현해보면 되겠구나' 라며 해 보고 있습니다.

 

일단 해커스랭크에서 JS 문제 끝내고 나면, 백준으로 다시 넘어가봐야 겠습니다.

뭔가 새로운게 나오니 좋긴 한데, 백준에서도 다시 해 보고 싶네요.

 

여담.

'아, 아직 안 본 부분이었구나' 라는 걸 알았습니다.

역시 일단 책 다보고 다시 도전해야 하려나 했지만, 일단 이것도 이것 나름대로 계속 해봐야 겠습니다.

아무튼 개념은 알고 있으니 적자면, 저 function 내부의 함수는 중첩함수다. 정도.


Objective

In this challenge, we practice using JavaScript classes. Check the attached tutorial for more details.

Task

Create a Polygon class that has the following properties:

  • A constructor that takes an array of integer values describing the lengths of the polygon's sides.
  • A perimeter() method that returns the polygon's perimeter.

Locked code in the editor tests the Polygon constructor and the perimeter method.

Note: The perimeter method must be lowercase and spelled correctly.

Input Format

There is no input for this challenge.

Output Format

The perimeter method must return the polygon's perimeter using the side length array passed to the constructor.

Explanation

Consider the following code:

// Create a polygon with side lengths 3, 4, and 5 let triangle = new Polygon([3, 4, 5]); // Print the perimeter console.log(triangle.perimeter());

When executed with a properly implemented Polygon class, this code should print the result of .

/*
 * 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());

 

여담 2.

credit : 이젤론의 창고지기

'Face the fear, build the future' 카테고리의 다른 글

Day 23 - Record  (0) 2021.07.23
알고리즘 풀이 - Day 15  (0) 2021.07.15
7월 10일 - 블로그 재정비, 그리고 남은 계획  (0) 2021.07.10
record - 6th July  (0) 2021.07.06
record - 3rd July  (0) 2021.07.03