Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Last active January 1, 2016 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Zegnat/8181101 to your computer and use it in GitHub Desktop.
Save Zegnat/8181101 to your computer and use it in GitHub Desktop.
/**
* 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