Skip to content

Instantly share code, notes, and snippets.

@0xCourtney
Created March 26, 2018 15:47
Show Gist options
  • Save 0xCourtney/541ea440209fd06ab993cc95a2eebbc0 to your computer and use it in GitHub Desktop.
Save 0xCourtney/541ea440209fd06ab993cc95a2eebbc0 to your computer and use it in GitHub Desktop.
SwapCase - ToyProblem
// 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