Algorithm/codewars

Exes and Ohs 답안

Bonita SY 2021. 3. 23. 19:49
728x90
반응형

문제)

www.codewars.com/kata/55908aad6620c066bc00002a/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

 

내가 푼 답)

function XO(str) {
  var lowerStr = str.toLowerCase();
  if (lowerStr.includes('x') || lowerStr.includes('o')) {
    var xCnt = 0;
    var oCnt = 0;
    lowerStr.split('').forEach(s => {
      if (s === 'x') {
        xCnt += 1;
      } else if (s === 'o') {
        oCnt +=1;
      }
    })
    
    if (xCnt === oCnt) {
      return true;
    } else {
      return false;
    }
  } else {
    return true;
  }
}

 

테스트 결과)

 

마음에 드는 다른 사람 소스)

728x90
반응형

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

Calculating with Functions 답안  (0) 2021.03.24
Is this a triangle? 답안  (0) 2021.03.23
You're a square! 답안  (0) 2021.03.23
Descending Order 답안  (0) 2021.03.23
Convert string to camel case 답안  (0) 2021.03.23