프로그래머스 연습문제 -최솟값 만들기(Level2, JS)

j_wisdom_h·2023년 1월 28일
0

CodingTest

목록 보기
27/58

프로그래머스 연습문제 -최솟값 만들기(Level2, JS)

문제설명


제한사항 & 입출력예


Solution

function solution(A, B) {
  A.sort((a, b) => a - b);
  B.sort((a, b) => b - a);
  return A.map((e, i) => e * B[i]).reduce((acc, curr) => acc + curr, 0);
}

공부한 것

Array.prototype.reduce()
: 메서드는 배열의 각 요소에 대해 주어진 리듀서 (reducer) 함수를 실행하고, 하나의 결과값을 반환

const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (accumulator, currentValue, index) => accumulator + currentValue,
  initialValue
);

console.log(sumWithInitial);
// Expected output: 10
profile
뚜잇뚜잇 FE개발자

0개의 댓글