<html>

<head>
  <meta charset="UTF-8">
  <title>출력결과</title>
</head>

<!-- A를 #으로 바꾸는 프로그램을 작성 -->

<body>
  <script>
    let str = "BANANA";

    function solution(str) {
      let answer ='';
      for (let x of str) {
        if (x == 'A') answer += '#';
        else answer += x;
      }
      return answer;
    }

    function solution2(str) {
      let answer = str;
      answer = answer.replace(/A/g, "#");
      return answer;
    }


    console.log(solution(str));
  </script>
</body>

</html>

solution2 는 정규식입니다.

/A/g  : /A/ 문자열을 찾는데 g를 붙여 전체로..

answer.replace(/A/g, "#") :  answer의 A 전부를 #으로 바꿉니다.

+ Recent posts