Skip to content

Instantly share code, notes, and snippets.

@giannispan
Created February 1, 2016 17:20
Show Gist options
  • Select an option

  • Save giannispan/261893ee46d24677fcf2 to your computer and use it in GitHub Desktop.

Select an option

Save giannispan/261893ee46d24677fcf2 to your computer and use it in GitHub Desktop.
Write a function toWeirdCase 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.
function toWeirdCase(string){
var stringWords = string.split(' ');
var length = stringWords.length;
for(var i = 0; i < length; i++)
{
stringWords[i] = wcase(stringWords[i]);
}
return stringWords.join(' ');
}
function wcase(str) {
var newString = '';
for( var c = 0; c < str.length; c++){
newString += c % 2 ? str[c].toLowerCase() : str[c].toUpperCase();
}
return newString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment