Created
February 1, 2016 17:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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