Permutation - What It Is.
In the unlikely scenario that the Teacher wanted to see just how any four pupils, in a class of six (6), could be seated on a four-person desk; what this Teacher would be doing in essence is called Permutation (nPr).
Code for Doing Permutation in JavaScript
The algorithm for Permutation - nPr, possible ways of arrangement - will simply be based on that of combination.
                        All that is needed after combination is a rotation / shuffle of
                        the make-up / constituents of each possible combination result.
                        
                        This shuffle simply involves interchanging the elements of the
                        combination group of size, r, to take all possible positions
                        starting from the extreme right to extreme left.
                    
                        This is how our Permutation code in JavaScript will work.
                        
                        Create 2 new files; On Notepad++: File, New.
                        Call them Permutation.html
 and Permutation.js
 respectively.
                        
                        Type out the adjoining JavaScript code for Permutation
                        (nPr).
                    
                        Advice: You might want to keep the mother-class size (n)
                        and the group-size (r) small to avoid the permutation code taking too long.
                        
                        As a rule-of-thump, DO NOT ASK QUESTIONS YOU DON'T WANT TO KNOW THE ANSWER TO.
                    
Permutation - .html
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Permutation</title>
<script src="Combination.js"></script>
<script src="Permutation.js"></script>
</head>
<body>
<h3>Possible Word/Letter/Object Arrangements</h3>
<!-- This is where the result will be displayed when it is ready.-->
<div id="word_perm"></div>
<script>
var result =
possibleWordPermutations(["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_perm").innerHTML +=
words.join(", ") + " permutation " + r + ":<br/><br/>" + print +
"<br/><br/>Number of ways is " + count + ".";
</script>
</body>
</html>
JavaScript Code for Permutation - .js
var perm_store = {};
var index;
// till the ground for shuffle to grind on
function possibleWordPermutations(candidates, size) {
var combination = possibleWordCombinations(candidates, size);
// illegal 'r' value
if (combination[0] === undefined || r == 1) {
perm_store = combination;
} else {
var i, n = 0;
for (i in combination) {
index = r - 1;
local_store = {0: [], 1: []};
// copy up last two elements of 'comb_store[i]'
local_store[0][0] = local_store[1][1] = comb_store[i][index--];
local_store[0][1] = local_store[1][0] = comb_store[i][index--];
if (r > 2) {
shuffleWord(local_store, i);
}
var m;
for (m in local_store) {
perm_store[n++] = local_store[m];
}
}
}
return perm_store;
}
function shuffleWord(arg_store, i) {
local_store = {};
var j, k = 0;
for (j in arg_store) {
var members = arg_store[j];
// add last 'origin' element to this list of members
members.push(comb_store[i][index]);
var shift_index = members.length;
var temp_char;
// shuffle this pack of words
while (shift_index > 0) {
var find = false, m;
// skip if already in store
for (m in local_store) {
if (JSON.stringify(local_store[m]) == JSON.stringify(members)) {
find = true;
break;
}
}
if (!find) {
local_store[k++] = members.slice(0);
}
// interchange these two neighbours
if (--shift_index > 0 && members[shift_index] != members[shift_index - 1]) {
temp_char = members[shift_index];
members[shift_index] = members[shift_index - 1];
members[shift_index - 1] = temp_char;
}
}
}
// Are there any elements left? repeat if yes
if (index-- > 0) {
shuffleWord(local_store, i);
}
}