[codekata] moveZero

EJ__OH·2021년 12월 5일
0

Q. 주어진 숫자 배열에서, 0을 배열의 마지막쪽으로 이동. 원래 있던 숫자의 순서는 바꾸지 말기.

새로운 배열을 생성해서는 안 됩니다.

A.

const moveZeroes = nums => {
    
  for(let i = 0; i < nums.length; i++){
    if(nums[i] === 0){
      nums.splice(i, 1);
      i--;
      nums.push(0);
    }
  }
  return nums;
}
profile
Junior FE Developer

0개의 댓글