이론
includes() 메서드는 배열의 항목에 특정 값이 포함되어 있는지를 판단하여 적절히 true 또는 false를 반환해요.
includes(searchElement[, fromIndex])
- searchElement: 배열에서 찾을 값
- fromIndex: 검색을 시작할 인덱스. 음수를 지정하면 배열의 끝에서부터 검색을 시작해요. 기본값은- 0이에요.
const array1 = [1, 2, 3];
 
console.log(array1.includes(2));
// Expected output: true
 
const pets = ['cat', 'dog', 'bat'];
 
console.log(pets.includes('cat'));
// Expected output: true
 
console.log(pets.includes('at'));
// Expected output: false
slice() 메서드는 어떤 배열의 begin 부터 end 까지(end 미포함)에 대한 얕은 복사본을 새로운 배열 객체로 반환해요. 원본 배열은 바뀌지 않아요.
arr.slice([begin[, end]])
- begin또는- end가 음수인 경우, 배열의 끝에서부터의 길이를 나타내요.
- begin이 생략된 경우,- 0번째 인덱스부터 추출해요.
- end가 생략된 경우, 배열의 끝까지 추출해요.
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
 
console.log(animals.slice(2));
// Expected output: Array ["camel", "duck", "elephant"]
 
console.log(animals.slice(2, 4));
// Expected output: Array ["camel", "duck"]
 
console.log(animals.slice(1, 5));
// Expected output: Array ["bison", "camel", "duck", "elephant"]
 
console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]
 
console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]
 
console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
indexOf() 메서드는 배열에서 주어진 요소를 찾을 수 있는 첫 번째 인덱스를 반환하고, 찾을 수 없는 경우 -1을 반환해요.
indexOf(searchElement[, fromIndex])
- searchElement: 배열에서 찾을 요소
- fromIndex: 검색을 시작할 인덱스. 음수인 경우 배열의 끝에서부터 요소를 찾아요. 기본값은 0이에요.
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
 
console.log(beasts.indexOf('bison'));
// Expected output: 1
 
// Start from index 2
console.log(beasts.indexOf('bison', 2));
// Expected output: 4
 
console.log(beasts.indexOf('giraffe'));
// Expected output: -1
코드
function solution(arr) {
  if (!arr.includes(2)) {
    return [-1];
  }
 
  return arr.slice(arr.indexOf(2), arr.lastIndexOf(2) + 1);
}