
정수 배열 numbers와 정수 num1, num2가 매개변수로 주어질 때, numbers의 num1번 째 인덱스부터 num2번째 인덱스까지 자른 정수 배열을 return 하도록 solution 함수를 완성해보세요.
2 ≤ numbers의 길이 ≤ 30
0 ≤ numbers의 원소 ≤ 1,000
0 ≤ num1 < num2 < numbers 의 길이
| numbers | num1 | num2 | result |
|---|---|---|---|
| [1, 2, 3, 4, 5] | 1 | 3 | [2, 3, 4] |
| [1, 3, 5] | 1 | 2 | [3, 5] |
:: Code ::
function solution(numbers, num1, num2) {
return numbers.slice(num1,num2+1)
}
✔️ Array.prototype.slice(startIndex, endIndex+1)
:: 🧷 Code 🧷 ::
function solution(numbers, num1, num2) {
return numbers.splice(num1, num2-num1+1);
}
✔️ Array.prototype.splice(startIndex, 삭제할 개수)
📌 Caution. - array.slice 와 array.splice 의 차이점
Array.prototype.slice( )
: 시작인덱스와 끝인덱스 +1 값을 받아 자른 배열을 반환한다.
Array.prototype.splice( )
: 시작인덱스와 시작인덱스부터 삭제할 개수를 받아 자른 배열을 반환한다.