Last active
January 1, 2016 17:59
-
-
Save Zegnat/8181101 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Create an array containing every possible way to write a given string in mixed-case. | |
* | |
* @param {String} str - A string to find every case-combination for. | |
* @return {Array} Every possible case-combination. | |
*/ | |
function mixedCase(str) { | |
'use strict'; | |
str = str.toLowerCase(); | |
var length = str.length, | |
outcomes = Math.pow(2, length), | |
limit = Math.pow(10, length), | |
zeroes = limit.toString().slice(1), | |
outcome = [], | |
map, | |
out, | |
counter; | |
while (outcomes--) { | |
map = outcomes.toString(2); | |
if (map < limit) { map = (zeroes + map).slice(-length); } | |
counter = length; | |
out = ''; | |
while (counter--) { | |
out = (map[counter] === '1' ? str[counter].toUpperCase() : str[counter]) + out; | |
} | |
outcome.push(out); | |
} | |
return outcome; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment