초보 개발자의 일기

대소문자 변환 본문

코딩테스트/JS 알고리즘 문제(JS)

대소문자 변환

판다꼬마 2022. 8. 11. 22:56
728x90

문제

 

 

입력

  • 첫 줄에 문자열이 입력된다. 문자열의 길이는 100을 넘지 않습니다.

 

출력

  • 첫 줄에 대문자는 소문자로, 소문자는 대문자로 변환된 문자열을 출력합니다.

입력 예시

StuDY

출력 예시

sTUdy

풀이 방법

toUpperCase()와 toLowerCase()를 이용해 대소문자를 판별한 후

조건문을 써서 바꾸었다.

내 코드

<html>
    <head>
        <meta charset="UTF-8">
        <title>출력결과</title>
    </head>
    <body>
        <script>
            function solution(s){  
                let result="";
                for(let x of s){
                    if(x===x.toUpperCase()) answer+=x.toLowerCase();
                    else answer+=x.toUpperCase();
                }
                return result;
            }

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

Solution

<html>
    <head>
        <meta charset="UTF-8">
        <title>출력결과</title>
    </head>
    <body>
        <script>
            function solution(s){  
                let answer="";
                for(let x of s){
                    if(x===x.toUpperCase()) answer+=x.toLowerCase();
                    else answer+=x.toUpperCase();
                }
                return answer;
            }

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

느낀점

대 소문자 판별하는 법을 몰라 검색해 풀었다.

728x90

'코딩테스트 > JS 알고리즘 문제(JS)' 카테고리의 다른 글

가운데 문자 출력  (0) 2022.08.12
가장 긴 문자열  (0) 2022.08.12
A를 #으로  (0) 2022.08.05
일곱 난쟁이  (0) 2022.08.05
10부제  (2) 2022.08.04