문자열을 받는 JavaScript 함수를 작성해야 합니다. 우리 함수의 임무는 다음 조건에 따라 문자열을 변경하는 것입니다 -
- 문자열의 첫 글자가 대문자이면 전체 문자열을 대문자로 변경해야 합니다.
- 그렇지 않으면 전체 문자열을 소문자로 변경해야 합니다.
예시
다음은 코드입니다 -
const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => { let newStr = ''; const isUpperCase = str[0].charCodeAt(0) >= 65 && str[0].charCodeAt(0) <= 90; if(isUpperCase){ newStr = str.toUpperCase(); }else{ newStr = str.toLowerCase(); }; return newStr; }; console.log(changeStringCase(str1)); console.log(changeStringCase(str2));
출력
다음은 콘솔의 출력입니다 -
THIS IS A NORMAL STRING thisisacamelcasedstring