Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Forked from jluismm2311/toWeirdCase.js
Created May 31, 2016 13:00
Show Gist options
  • Save manjeettahkur/945940a0359c08f3d8bf66847248ef83 to your computer and use it in GitHub Desktop.
Save manjeettahkur/945940a0359c08f3d8bf66847248ef83 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