BrettBits > Java and Javascript Programs > Character/Word Combinations Character/Word Combinations
To use this Javascript for your own, copy the contents of the textbox below, and paste them into Notepad.
Then, save the contents as a .htm or .html file. The Javascript is now yours! Just ensure you give me proper
credit for creating it.
Javascript Source Code
<script type="text/javascript">
<!--
//Author: Brett McLean
//Email Address: brettbits@hotmail.com
//Website: http://www.brettbits.com/
//Description: This Javascript will accept a string of letters as input and determine how many ways
//those letters can be rearranged, ignoring non-unique combinations caused by repeat letters.
//As well, at the user's request, this program will recursively calculate and print each
//of these combinations.
//For example, an input of "abc" will return that 6 possible combinations exist, and
//that they are "abc, acb, bac, bca, cab, cba".
//calculateCombinations takes inputText as a parameter, which is a string containing all
//user-provided characters. It calculates how many combinations exist for these characters,
//prints them in the outputField, as well as returning the number to the calling function.
function calculateCombinations(inputText)
{
inputCopy = inputText;
numerator = factorial(inputText.length);
denominator = 1;
while(inputCopy != "") {
denominator *= factorial(howManyOccurances(inputCopy, inputCopy.charAt(0)));
inputCopy = stripCharactersFromString(inputCopy, inputCopy.charAt(0));
}
document.combform.outputField.value = (numerator/denominator);
return numerator/denominator;
}
//HowManyOccurances returns how many times characterToFind appears in stringToSearch.
//Example: howManyOccurances("abcabcabca", "a") returns 4.
function howManyOccurances(stringToSearch, characterToFind) {
count = 0;
for(i = 0; i < stringToSearch.length; i++) {
if(stringToSearch.charAt(i) == characterToFind)
count++;
}
return count;
}
//stripCharactersFromString returns stringToStripFrom, with all occurances
//of characterToStrip removed.
//Example: stripCharactersFromString("abcabcabca", "a") returns "bcbcbc".
function stripCharactersFromString(stringToStripFrom, characterToStrip) {
outputString = "";
for(i = 0; i < stringToStripFrom.length; i++)
if(stringToStripFrom.charAt(i) != characterToStrip)
outputString += stringToStripFrom.charAt(i);
return outputString;
}
//factorial returns the factorial of inputNumber, also written as (inputNumber)!.
//Definition of factorial: inputNumber! = (inputNumber) * (inputNumber-1) * ... * 2 * 1
//Example: factorial(4) = 4! = 4 * 3 * 2 * 1 = 24
function factorial(inputNumber) {
outputNumber = 1;
for(i = inputNumber; i > 0; i -= 1)
outputNumber *= i;
return outputNumber;
}
//createWordList recursively generates an array containing all possible combinations of
//a given string of letters. It solves this problem by taking each letter in the word,
//and then appending to it all possible combinations of the remaining letters.
//Initially, wordArray should be empty, strbase should equal "", and remainingchars
//should contain the user's input string.
function createWordList(wordArray, strbase, remainingchars) {
if(remainingchars.length == 1) { //base case: only one character remains, and can be arranged only one way
wordArray[wordArray.length] = strbase + remainingchars.charAt(0); //append one remaining character to string base
} else {
for(var j = 0; j < remainingchars.length; j++) {
currchar = remainingchars.charAt(j); //first character of remaining characters
if(remainingchars.indexOf(currchar) == j) //ensures repeat letters do not cause combination redundancy
wordArray = createWordList(wordArray, strbase + remainingchars.charAt(j), remainingchars.substring(0, j) + remainingchars.substring(j+1, remainingchars.length)); //recursive call
}
}
return wordArray; //returns the wordArray containing stbase plus all possible combinations of remainingchars
}
//printWordList accepts an array, and prints each string contained within, separated by commas.
//It then places this output into the textarea named "combinationlist".
function printWordList(wordArray)
{
output = "";
for(i = 0; i < wordArray.length-1; i++)
output += wordArray[i] + ", ";
output += wordArray[wordArray.length-1];
document.combform.combinationlist.value = output;
}
//showCombinations receives the user's input characters as a parameter, calculates how many combinations exist from these
//characters, and then displays every one of these combinations. This function has a safeguard in which it warns
//the user if a calculation will take a long time to process.
function showCombinations(inputText)
{
numCombinations = calculateCombinations(inputText);
confirmMessage = "The following calculation will involve finding " + numCombinations + " different combinations of the letters";
confirmMessage += "\nyou have provided. Depending on the speed of your computer, it may take a while to";
confirmMessage += "\nprocess this request, or in a worst-case scenario, your web browser may crash.";
confirmMessage += "\nThis will do no damage to your computer that a restart will not solve.";
confirmMessage += "\nAre you sure you want to continue?";
if(numCombinations < 5000 || confirm(confirmMessage)) //only do it if the number of combinations to calculate is low, or the user agrees to the warning
printWordList(createWordList(new Array(), "", inputText)); //calculate and then print all combinations of the letters provided
}
// -->
</script>
<form name="combform" onsubmit="calculateCombinations(inputField.value); return false;" action="">
<table>
<tr><td align="center">
Enter letters here: <input type="text" name="inputField" />
</td></tr>
<tr><td align="center">
<input type="submit" value="How Many Combinations Are There?" />
</td></tr>
<tr><td align="center">
Your letters can be arranged <input type="text" name="outputField" size="10" /> unique ways.
</td></tr>
<tr><td align="center">
<textarea name="combinationlist" rows="10" cols="70">List of character combinations are displayed here.</textarea>
</td></tr>
<tr><td align="center">
<input type="button" onclick="showCombinations(inputField.value);" value="List All Possible Combinations" />
</td></tr>
</table>
</form>
Alternatively, you can download the source .html file itself by right-clicking on the Download button below, and click
"Save Target As..." or "Save Link As...":
Random BrettBits Stuff
Newest BrettBits Stuff
Random Quote
Current Projects