Skip to content

Instantly share code, notes, and snippets.

@areagray
Last active September 27, 2022 08:42
Show Gist options
  • Save areagray/8494508 to your computer and use it in GitHub Desktop.
Save areagray/8494508 to your computer and use it in GitHub Desktop.
Coderbyte: Have the function SwapCase(str) take the str parameter and swap the case of each character. For example: if str is "Hello World" the output should be hELLO wORLD. Let numbers and symbols stay the way they are.
//coderbyte
function SwapCase(str) {
var newChar='',
newString='';
newString = str.replace(/./g, function(myChar){
//use the replace to iterate and rebuild string
if (myChar.match(/[a-z]/)){
newChar=myChar.toUpperCase();
} else if (myChar.match(/[A-Z]/)){
newChar=myChar.toLowerCase();
} else {
newChar=myChar;
}
return newChar;
});
return newString;
}
@muhomerdogu
Copy link

Thanks dear ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment