https://school.programmers.co.kr/learn/courses/30/lessons/86491
명함은 돌려서 넣을 수 있으므로 가로/세로가 고정된 것이 아니다. 그니까 한 명함의 가로세로 길이를 정렬한 뒤에, 정렬된 2차 배열들의 [][0]들과 [][1]의 최대값만 곱해주면 되겠군!
function solution(sizes) {
let wmax=0;
let hmax=0;
let arr=[];
sizes.map(e=>{
e.sort((a,b)=>a-b);
arr.push(e);
});
arr.forEach(e=>{
if(e[0]>wmax){
wmax=e[0];
}
if(e[1]>hmax){
hmax=e[1];
}
});
return wmax*hmax;
}
function solution(sizes) {
const rotated = sizes.map(([w, h]) => w < h ? [h, w] : [w, h]);
let maxSize = [0, 0];
rotated.forEach(([w, h]) => {
if (w > maxSize[0]) maxSize[0] = w;
if (h > maxSize[1]) maxSize[1] = h;
})
return maxSize[0]*maxSize[1];
}
배열이나 객체의 속성을 해체하여 그 값을 개별 변수에 담을 수 있게 하는 자바스크립트의 표현식.
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// Expected output: 10
console.log(b);
// Expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// Expected output: Array [30, 40, 50]
a와 b에 각각 10과 20이 할당되었고,
rest에 30, 40, 50이 할당되었다.
var foo = ["one", "two", "three"];
var [red, yellow, green] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // "three"
가장 유용하게 사용하는 것은 두 변수의 값을 교환하는 것이다! 보통은 두 값을 교환하려면 임시 변수가 필요했는데, 구조분해할당을 사용한다면 임시 변수가 필요 없다.
var a = 1;
var b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
더 자세한 설명은 아래 사이트에서 확인 가능
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment