Skip to content

Instantly share code, notes, and snippets.

@cefaijustin
Created June 2, 2018 03:50
Show Gist options
  • Save cefaijustin/4fcf3eac49b19bf950d90bcf80771ca0 to your computer and use it in GitHub Desktop.
Save cefaijustin/4fcf3eac49b19bf950d90bcf80771ca0 to your computer and use it in GitHub Desktop.
To Weird Case
// 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 passed in string will only consist of alphabetical characters and spaces(' '). Spaces will only be present if there are multiple words. Words will be separated by a single space(' ').
function toWeirdCase(string){
return string.split(' ').map(function(word){
return word.split('').map(function(letter, index){
if (index % 2 === 0) {
return letter.toUpperCase();
} else if (index % 2 != 0) {
return letter.toLowerCase();
}
}).join('');
}).join(' ');
}
describe('toWeirdCase', function(){
it('should return the correct value for a single word', function(){
Test.assertEquals(toWeirdCase('This'), 'ThIs');
Test.assertEquals(toWeirdCase('is'), 'Is');
});
it('should return the correct value for multiple words', function(){
Test.assertEquals(toWeirdCase('This is a test'), 'ThIs Is A TeSt');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment