최댓값 만들기 (2)
정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소 중 두 개를 곱해 만들 수 있는 최댓값을 return하도록 solution 함수를 완성해주세요.
numbers | result |
---|---|
[1, 2, -3, 4, -5] | 15 |
[0, -31, 24, 10, 1, 9] | 240 |
[10, 20, 30, 5, 5, 20, 5] | 600 |
두 수의 곱중 최댓값은 -3 * -5 = 15 입니다.
두 수의 곱중 최댓값은 10 * 24 = 240 입니다.
두 수의 곱중 최댓값은 20 * 30 = 600 입니다.
function solution(numbers) {
let max = numbers[0] * numbers[1];
for(let i = 0; i < numbers.length - 1; i++){ //첫번째 원소이므로 조건식이 다름
const first = numbers[i];
for(let j = i + 1; j < numbers.length; j++){ //두번째 원소이므로 시작값이 다름
const second = numbers[j];
if(max < first * second){
max = first * second;
}
}
}
return max;
}