Skip to content
Advertisement

Find all lowercase and uppercase combinations of a string in Javascript

I am looking for this stackoverflow question to be answered in Javascript.

So if my input is “word”, the function should return:

word, Word, wOrd, WOrd, woRd, WoRd, etc..

here’s what i have so far but it only produces the permutations (doesn’t capitalize anything)

JavaScript

Advertisement

Answer

One option would be generating capitalization permutations via binary logic.

As a simple example of the snippet below, consider the following table where the left column is the binary representation of the current permutation and the right column is the resulting capitalization:

JavaScript

JavaScript

Note that theoretically this approach would work all the way up to Number.MAX_SAFE_INTEGER, up to inputs of length 52, but realistically you’ll run into performance issues.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement