π λ¬Έμ
- μ μ λ°°μ΄ numbersκ° μ£Όμ΄μ§λλ€. numbersμμ μλ‘ λ€λ₯Έ μΈλ±μ€μ μλ λ κ°μ μλ₯Ό λ½μ λν΄μ λ§λ€ μ μλ λͺ¨λ μλ₯Ό λ°°μ΄μ μ€λ¦μ°¨μμΌλ‘ λ΄μ return νλλ‘ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
https://programmers.co.kr/learn/courses/30/lessons/68644
π λ¬Έμ νμ΄
function solution(numbers) {
const temp = [];
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
temp.push(numbers[i] + numbers[j]);
}
}
const answer = [...new Set(temp)];
return answer.sort((a, b) => a - b);
}
π¬ μ 리
new Set
: λ°°μ΄μ νΉμ μμκ° ν¬ν¨λμ΄ μλμ§ μ¬λΆ
- new Set(temp) μ€λ³΅κ°μ΄ μλ λ°°μ΄μ Set κ°μ²΄λ‘ λ§λ€μ΄μ μ€λ³΅κ° μ κ±°
- [...new Set(temp)] : μ κ° μ°μ°μλ₯Ό ν΅ν΄ Set κ°μ²΄λ₯Ό λ°°μ΄λ‘ λ³ν
answer.sort((a, b) => a - b);
: μ€λ¦μ°¨μ μ λ ¬
- answer.sort((a, b) => b - a); : λ΄λ¦Όμ°¨μ μ λ ¬