5_hyun 2022. 8. 15. 16:14
반응형

 

해결법

isNaN 함수를 활용해야한다. 반복문을 돌면서 isNaN 함수에 문자를 넣었을 때 false가 나오면 숫자이다. 따라서 ! 연산자를 이용해서 숫자를 answer에 넣어주면 된다.

 

코드

<html>
  <head>
    <meta charset="UTF-8" />
    <title>출력결과</title>
  </head>
  <body>
    <script>
      function solution(str) {
        let answer = "";

        for (let i of str) {
          if (!isNaN(i)) {
            answer += i;
          }
        }

        return parseInt(answer);
      }

      let str = "g0en2T0s8eSoft";
      console.log(solution(str));
    </script>
  </body>
</html>
반응형