Lv.K番目の数


🤖質問する


👍🏻 2022年1月22日

私が作ったコード

<script>
  function solution(array, commands) {
      let answer = [];
      let value;
      commands.forEach((command, i) => {
          let startIdx = command[0] - 1;
          let endIdx = command[1];
          let nIdx = command[2] - 1;

          let sliceArr = array.slice(startIdx, endIdx);;
          sliceArr.sort((a,b) => a - b);
          value = sliceArr[nIdx]
          answer.push(value)
      })
      return answer;
  }
</script>

新学のコード

<script>
  function solution(array, commands) {
      let answer = [];
      let value;
      commands.forEach((command, i) => {
          const [sPosition, ePosition, n] = command

          let sliceArr = array.slice(sPosition-1, ePosition);;
          sliceArr.sort((a,b) => a - b);
          value = sliceArr[n-1]
          answer.push(value)
      })
      return answer;
  }
</script>
構造分解配分が自由に使える日まで頑張ります!👍