Algorithm/codewars

Moving Zeros To The End 답안

Bonita SY 2021. 3. 24. 20:53
728x90
반응형

문제)

www.codewars.com/kata/52597aa56021e91c93000cb0/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답)

var moveZeros = function (arr) {
  let zeroCnt = 0;
  const result = arr.filter(elem => {
    if (elem === 0) {
      zeroCnt += 1;
      return false;
    } else {
      return true;
    }
  });
  
  for (let i=0; i<zeroCnt; i++) {
    result.push(0);
  }
  
  return result;
}

 

테스트 결과)

 

마음에 드는 남이 푼 답)

ㅋㅋㅋㅋ 재밌게 풀었네

728x90
반응형

'Algorithm > codewars' 카테고리의 다른 글

Recover a secret string from random triplets 답안  (0) 2021.03.29
Greed is Good 답안  (0) 2021.03.24
Where my anagrams at? 답안  (0) 2021.03.24
Equal Sides Of An Array 답안  (0) 2021.03.24
Are they the "same"? 답안  (0) 2021.03.24