인프런 커뮤니티 질문&답변
배열과 메서드를 이용하여 풀어봤습니다.
작성
·
200
0
방법1.
function solution(a, b, c){
let answer;
let arr = [a,b,c];
let max = Math.max(...arr);
let min = Math.min(...arr);
let filterArr = arr.filter(num => (num < max && num > min));
if ( (min + filterArr[0] ) > max) {
answer = 'yes'
} else {
answer = 'no'
}
return answer;
}console.log(solution(13,33,17));
방법2.
function solution(a, b, c){
let answer;
let arr = [a,b,c];
arr.sort((x, y) => x - y);
if ( (arr[0] + arr[1]) > arr[2]) {
answer = 'yes'
} else {
answer = 'no'
}
return answer;
}
console.log(solution(13,33,17));
강의를 보니 쉬운방법을 두고 굳이 어려운 방법으로 푼 느낌이네요 ㅠㅠ
퀴즈
세 수 중 최솟값을 찾을 때, if 문만 사용한다면 어떤 방식으로 비교하는 것이 일반적인가요?
세 수를 한 번에 비교하여 가장 작은 수를 바로 찾습니다.
두 수의 최솟값을 먼저 찾고, 그 결과와 나머지 한 수를 비교합니다.
가장 큰 수를 먼저 찾은 후, 남은 두 수 중 작은 값을 찾습니다.
모든 가능한 쌍을 비교하여 가장 작은 값을 찾습니다.





