(정렬)
function solution(array, commands) {
let answer = [];
for(let i of commands){
let arraySlice = array.slice(i[0]-1, i[1]).sort((a,b) => a - b);
answer.push(arraySlice[i[2]-1]);
}
return answer;
}
function solution(array, commands) {
return commands.map(command => {
const [sPosition, ePosition, position] = command
const newArray = array
.filter((value, fIndex) => fIndex >= sPosition - 1 && fIndex <= ePosition - 1)
.sort((a,b) => a - b)
return newArray[position - 1]
})
}
나는 배열을 자르고 난 후 정렬하여 해당 숫자를 answer 배열에 추가했다.
다른 답은
map 함수를 통해 command에 시작 위치, 종료 위치, 반환할 위치를 구조 분해하여 변수로 추출한다.
그 후 배로운 배열에 필터를 적용하여 조건에 맞는 요소만 선택하고, sort를 통해 정렬한다.
그 이후 원하는 값을 return 했다.