(1) X : 일부 테스트케이스 실패
function solution(array, n) {
const minDiff = Math.min(...array.map(num => Math.abs(num - n)))
return array.filter(num => Math.abs(num - n) === minDiff)[0]
}
(2) O
function solution(array, n) {
const minDiff = Math.min(...array.map(num => Math.abs(num - n)))
return array.filter(num => Math.abs(num - n) === minDiff).sort((a, b) => a - b)[0]
}
function solution(array, n) {
const minDiff = Math.min(...array.map(a => Math.abs(a - n)));
return array.sort((a, b) => a - b).find(a => Math.abs(a - n) === minDiff);
}