문제(출처: 프로그래머스)

문자열 my_string이 매개변수로 주어질 때, 대문자는 소문자로 소문자는 대문자로 변환한 문자열을 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

function solution(my_string) {
    var answer = '';
    const lowerCase= /^[a-z]/;
    my_string.split('').map(w =>{
        answer += lowerCase.test(w) ? w.toUpperCase() : w.toLowerCase() 
    })
    return answer;
}

 

 다른 유저가 푼 방식

// 유저 1 정규 표현식 을 사용 안하고 적용
function solution(my_string) {
    return my_string.split('').map(n => 
    n === n.toUpperCase() ? n.toLowerCase() : n.toUpperCase()).join('')
}

 

 배운 것들

     -  대문자 소문자를 확인 할때 정규 표현식을 사용하지 않아도 가능한 방식

+ Recent posts