Created
March 26, 2018 15:47
-
-
Save 0xCourtney/541ea440209fd06ab993cc95a2eebbc0 to your computer and use it in GitHub Desktop.
SwapCase - ToyProblem
This file contains hidden or 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
// Swap Case | |
// Below is a function that takes in a string and reverses the case of every character and returns the new string. | |
// It is currently in a broken state and does not run properly. | |
// It is possible to fix the code by only modifying the existing code, not adding new lines. | |
//test data | |
//'This Is An Example' becomes 'tHIS iS aN eXAMPLE' | |
//'boB rOss IS thE OrIgInAl GanGster' Becomes 'BOb RoSS is THe oRiGiNaL gANgSTER' | |
function caseReverse(str) { | |
var strArray = str.split(""); | |
for (var i = 0; i < strArray.length; i++) { | |
if (strArray[i] === strArray[i].toUpperCase()) { | |
strArray[i] = strArray[i].toLowerCase(); | |
} else { | |
strArray[i] = strArray[i].toUpperCase(); | |
} | |
} | |
return strArray.join(""); | |
} | |
console.log(caseReverse("boB rOss IS thE OrIgInAl GanGster")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment