문자열을 받아서 원래 문자열의 모든 대문자를 소문자로 변환하고 모든 소문자를 원래 문자열에서 대문자로 변환하여 새 문자열을 구성하는 JavaScript 함수를 작성해야 합니다.
예:문자열이 -
인 경우const str = 'The Case OF tHis StrinG Will Be FLiPped';
출력
그러면 출력은 다음과 같아야 합니다. -
const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED';
예시
이에 대한 코드는 -
const str = 'The Case OF tHis StrinG Will Be FLiPped';
const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0)<= 90;
const isLowerCase = char => char.charCodeAt(0) >= 97 && char.charCodeAt(0) <= 122;
const flipCase = str => {
let newStr = '';
const margin = 32;
for(let i = 0; i < str.length; i++){
const curr = str[i];
if(isLowerCase(curr)){
newStr += String.fromCharCode(curr.charCodeAt(0) - margin);
}else if(isUpperCase(curr)){
newStr += String.fromCharCode(curr.charCodeAt(0) + margin);
}else{
newStr += curr;
};
};
return newStr;
};
console.log(flipCase(str)); 출력
콘솔의 출력 -
tHE cASE of ThIS sTRINg wILL bE flipped