Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
Created September 11, 2015 22:18
Show Gist options
  • Save jluismm2311/ac9e3280c11962f9fbc5 to your computer and use it in GitHub Desktop.
Save jluismm2311/ac9e3280c11962f9fbc5 to your computer and use it in GitHub Desktop.
Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the zero-ith index is even, therefore that character should be upper cased. The passe…
function toWeirdCase(string){
let newString = string.split(' ');
for(let i = 0; i < newString.length; i++){
newString[i] = newString[i].weird();
}
return newString.join(' ');
}
String.prototype.weird = function( ){
var newString = '';
for(let i = 0; i< this.length ; i++){
newString += (i % 2 === 0)? this[i].toUpperCase():this[i].toLowerCase();
}
return newString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment