문제(출처: 프로그래머스)

2차원 좌표 평면에 변이 축과 평행한 직사각형이 있습니다. 직사각형 네 꼭짓점의 좌표 [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]가 담겨있는 배열 dots가 매개변수로 주어질 때, 직사각형의 넓이를 return 하도록 solution 함수를 완성해보세요.

 

▶ 내가 푼 방식

function solution(dots) {
    const xArray = dots.map(v=> v[0])
    const yArray = dots.map(v=> v[1])
    const xLength = Math.max(...xArray) - Math.min(...xArray)
    const yLength = Math.max(...yArray) - Math.min(...yArray)

    return xLength * yLength
}

 

 다른 유저가 푼 방식

... 이번에는 다들 비슷하게 풀이함

 

 배운 것들

     -  최대 최소 값을 구하는 방법

const temp = [1,23,4,10]
const maxValue1 = Math.max.apply(null, temp)
const maxValue2 = Math.max(...temp)

const maxValue3 = array.reduce( function (previous, current) { 
	return previous > current ? previous:current;
});

const minValue3 = array.reduce( function (previous, current) { 
	return previous > current ? current:previous;
});

     - 

+ Recent posts