본문 바로가기
CS (컴퓨터 사이언스)/Algorithm (알고리즘)

[알고리즘] 10. 문자찾기 / 11. 대문자찾기 / 12. 대문자로 통일 / 13. 대소문자 변환 / 14. 가장 긴 문자열 / 15. 가운데 문자 출력 / 16. 중복문자제거 / 17. 중복단어제거

by dreamer10457 2024. 1. 28.
반응형

10. 문자찾기 

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

        for (const ele of s) {
          if (ele === t) answer++;
        }

        return answer;
      }

      let str = 'COMPUTERPROGRAMMING';
      console.log(solution(str, 'R'));
    </script>
  </body>
</html>

 

11. 대문자찾기 

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

        for (let ele of s) {
          if (ele === ele.toUpperCase()) {
            answer++;
          }
        }

        return answer;
      }

      let str = 'KoreaTimeGood';
      console.log(solution(str));
    </script>
  </body>
</html>

 

12. 대문자로 통일

<html>
  <head>
    <meta charset="UTF-8" />
    <title>출력결과</title>
  </head>
  <body>
    <script>
      function solution(s) {
        return s.toUpperCase();
      }

      let str = 'ItisTimeToStudy';
      console.log(solution(str));
    </script>
  </body>
</html>

 

13. 대소문자 변환

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

        for (let ele of s) {
          const codeNum = ele.charCodeAt();

          if (ele === ele.toUpperCase()) {
            answer += ele.toLowerCase();
          } else {
            answer += ele.toUpperCase();
          }

          //   if (codeNum >= 65 && codeNum <= 90) {
          //     answer += ele.toLowerCase();
          //   } else {
          //     answer += ele.toUpperCase();
          //   }
        }

        return answer;
      }

      console.log(solution('StuDY'));
    </script>
  </body>
</html>

 

14. 가장 긴 문자열

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

        for (let word of s) {
          if (maxLength < word.length) {
            answer = word;
            maxLength = word.length;
          }
        }

        return answer;
      }
      let str = ['teacher', 'time', 'student', 'beautiful', 'good'];
      console.log(solution(str));
    </script>
  </body>
</html>

 

15. 가운데 문자 출력 

<html>
  <head>
    <meta charset="UTF-8" />
    <title>출력결과</title>
  </head>
  <body>
    <script>
      function solution(s) {
        // arr.slice([begin[, end]])
        // array.splice(start[, deleteCount[, item1[, item2[,
        let answer;

        const mid = Math.floor(s.length / 2);
        //홀수일 때
        if (s.length % 2 === 1) {
          answer = s.slice(mid, mid + 1);
        }
        //짝수일 때
        else {
          answer = s.slice(mid - 1, mid + 1);
        }

        //짝수일때
        return answer;
      }
      console.log(solution('stu'));
    </script>
  </body>
</html>

 

16. 중복문자제거 

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

        const map = new Map();

        for (const ele of s) {
          if (!map.has(ele)) {
            map.set(ele, true);
            answer += ele;
          }
        }
        console.log(map);

        return answer;
      }
      console.log(solution('ksekkset'));
    </script>
  </body>
</html>

 

17. 중복단어제거

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

        const map = new Map();

        for (let word of s) {
          if (!map.has(word)) {
            map.set(word, true);
            answer.push(word);
          }
        }

        return answer;
      }
      let str = ['good', 'time', 'good', 'time', 'student'];
      console.log(solution(str));
    </script>
  </body>
</html>
반응형