function solution(arr, commands) {
let answer=[];
for(let x of commands){
let t = [];
for(let i=x[0]-1;i<x[1];i++){
t.push(arr[i]);
}
t = t.sort((a,b)=>a-b);
answer.push(t[x[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]
})
}
const [sPosition, ePosition, position] = command
구조 분해 할당을 써서 표현할 수도 있다.
function solution(array, commands) {
return commands.map(v => {
return array.slice(v[0] - 1, v[1]).sort((a, b) => a - b).slice(v[2] - 1, v[2])[0];
});
}
slice함수를 이용