String/Number 리터럴 배열을 첫 번째 인수로, String/Number를 두 번째 인수로 취하는 JavaScript 함수를 작성해야 합니다.
두 번째 인수로 취한 변수가 배열에 없으면 -1을 반환해야 합니다.
그렇지 않고 배열에 숫자가 있으면 배열이 반전되었을 때 숫자가 차지했을 위치의 인덱스를 반환해야 합니다. 실제로 배열을 뒤집지 않고 그렇게 해야 합니다.
그런 다음 마침내 이 함수를 Array.prototype 객체에 연결해야 합니다.
예를 들어 -
[45, 74, 34, 32, 23, 65].reversedIndexOf(23); Should return 1, because if the array were reversed, 23 will occupy the first index.
예시
다음은 코드입니다 -
const arr = [45, 74, 34, 32, 23, 65];
const num = 23;
const reversedIndexOf = function(num){
const { length } = this;
const ind = this.indexOf(num);
if(ind === -1){
return -1;
};
return length - ind - 1;
};
Array.prototype.reversedIndexOf = reversedIndexOf;
console.log(arr.reversedIndexOf(num)); 출력
이것은 콘솔에서 다음과 같은 출력을 생성합니다 -
1