Last active
September 27, 2022 08:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks dear ...