1115 지나간 문제도 다시보자

냐하호후·2021년 11월 15일
0

대소문자 변환

StuDY를 STUdy로 변환하기

function solution(s){ 
      let answer = ''
             for(let i = 0; i < s.length; i++){
                 if(s[i] === s[i].toLowerCase()) answer += s[i].toUpperCase()
                 if(s[i] === s[i].toUpperCase())  answer += s[i].toLowerCase()
             }
             return answer
            }

            console.log(solution("StuDY"));

가장 긴 문자열

function solution(s){  
            //가장 긴 길이의 요소 리턴
            let longest = ''
            for(let i = 0; i < s.length; i++){
                if(s[i].length > longest.length){
                    longest = s[i]
                  }
                }
                return longest
            }
            let str=["teacher", "time", "student", "beautiful", "good"];
            console.log(solution(str));

중복문자 제거

 function solution(s){  
        //16. 중복문자제거
        let arr = []
        let answer = ''
        for(let x of s){
         //만약 arr의 요소중에 특정 글자가 없다면 추가한다. 있다면 continue...?
         if(arr.includes(x)) continue 
         else arr.push(x)
        }
        
        for(let i of arr){
         answer += i
         }
        return answer
     }
 console.log(solution("ksekkset"));

쉬운 문제도 오랜만에 보니까 기억이 안날수가있다는걸 새삼 느낀다 ㅎㅎ.. 많이 부족하니까 알고리즘을 더 열심히 파야겠다.

profile
DONE is better than PERFECT

0개의 댓글