우리는 배열 함수를 작성해야 합니다. 예를 들어 pushAtFalsy() 함수는 배열과 요소를 가져와야 합니다. 배열에서 찾은 첫 번째 거짓 인덱스에 요소를 삽입해야 합니다.
공백이 없으면 배열의 마지막에 요소를 삽입해야 합니다.
먼저 빈 위치의 인덱스를 검색한 다음 해당 값을 제공된 값으로 바꿉니다.
예시
다음은 코드입니다 -
const arr = [13, 34, 65, null, 64, false, 65, 14, undefined, 0, , 5, ,
6, ,85, ,334];
const pushAtFalsy = function(element){
let index;
for(index = 0; index < this.length; index++){
if(!arr[index] && typeof arr[index] !== 'number'){
this.splice(index, 1, element);
break;
};
};
if(index === this.length){
this.push(element);
}
};
Array.prototype.pushAtFalsy = pushAtFalsy;
arr.pushAtFalsy(4);
arr.pushAtFalsy(42);
arr.pushAtFalsy(424);
arr.pushAtFalsy(4242);
arr.pushAtFalsy(42424);
arr.pushAtFalsy(424242);
arr.pushAtFalsy(4242424);
console.log(arr); 출력
이것은 콘솔에서 다음과 같은 출력을 생성합니다 -
[ 13, 34, 65, 4, 64, 42, 65, 14, 424, 0, 4242, 5, 42424, 6, 424242, 85, 4242424, 334 ]