usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Code for Combination (Selection Without Repetition) in JavaScript



Code for Doing Combination in JavaScript

Writing up an algorithm to carry out the different Combination - Selection without Repetition, nCr - of a set of things in JavaScript requires some level of imaginative thinking.

Get a writing pad and pencil:
  1. Write out all n members in the set - for Combination - at the top of the pad.
  2. Beginning with the first member, match it separately with the other members until the required selected-group size (r) is reached.
  3. When every possible Combination for this first member is exhausted, remove the current first member from the mother set.
    The immediate next member becomes the new first member in the culminating set.
  4. Take the first member in what is left of the mother set and repeat the same process from step II.

How to carry out combination

This is exactly what we will do with code to list up all possible selections without repetition in JavaScript.

Create 2 new files; On Notepad++: File, New.
Call them Combination.html and Combination.js respectively.
Type out the adjoining JavaScript code for the combination of different options (nCr).



Why Bother About Combination

Well, isn't it obvious?
Say you are to pick only four (4) pupils from a class of six - such a small class; our little Combination algorithm solves this little problem for you by showing all your possible options / selection outcomes.







Combination - .html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Word Combination</title>
        <script src="Combination.js"></script>
    </head>
    <body>

        <h3>Selection Without Repetition</h3>
        <!-- This is where the result will be displayed when it is ready.-->
        <div id="word_combo"></div>

        <script>
            var result =
                    possibleWordCombinations(["Eno""Chidi""Olu""Ahmed""Osas""Gbeda"]3);
            var print = "", set, count = 0;
            for (set in result) {
                print += ++count + ": [" + result[set].join(", ") + "]<br/>";
            }
            document.getElementById("word_combo").innerHTML +=
                    words.join(", ") + " combination " + r + ":<br/><br/>" + print +
                    "<br/><br/>Number of ways is " + count + ".";
        </script>

    </body>
</html>

JavaScript Code for Combination - .js

var words, r; // min length of word
var comb_store = {}, c_index = 0;
var i;

// point of entry
function possibleWordCombinations(candidates, size) {
    words = candidates;
    r = size;
    i = 0;
    // check for conformity
    if (<= 0 || r > words.length) {
        comb_store = {};
    } else if (== 1) {
        for (; i < words.length; i++) {
            comb_store[c_index] = [];
            comb_store[c_index++].push(words[i]);
        }
    } else {
        progressiveCombination();
    }
    return comb_store;
}

// do combinations for all 'words' element
function progressiveCombination() {
    repetitivePairing([words[i]], i + 1);
    if (+ r <= words.length) {
        // move on to next degree
        i++;
        progressiveCombination();
    }
}

// do all possible combinations for 1st element of this array
function repetitivePairing(prefix, position) {
    var auxiliary_store = {};
    for (var j = 0; position < words.length; position++, j++) {
        // check if desired -- r -- size will be realised
        if (- prefix.length <= words.length - position) {
            auxiliary_store[j] = prefix.slice(0);
            auxiliary_store[j].push(words[position]);
            if (auxiliary_store[j].length < r) {
                // see to adding next word on
                repetitivePairing(auxiliary_store[j], position + 1);
            } else {
                comb_store[c_index++] = auxiliary_store[j];
            }
        }
    }
}



<< PreviousNext >>