Posts

Building a function that helps to find the total possible combinations of a set with Javascript

avatar of @gotgame
25
0 views
·
3 min read

In mathematics, the idea of combination according to Wikipedia can be defined as a selection of items from a set that has distinct members, such that the order of selection does not matter (unlike permutations).

A combination, basically in my own terms is a rearrangement of elements of one set randomly but with a finite number of possible rearrangements per element and a preset rule that dictates how the elements are arranged.

That's not an exactly thorough definition but you should get a basic picture of what we are trying to do now.

But for clarity sake, you can check out an example I got from BetterExplained

Let's say we need to pick a team of three people from a group of 10 people.

Now we have a variable value x which represents the total number of available options to pick from and that is 10.

And we also have another constant value y which is equal to 3 and is the number of people to select for the team.

Take note that the value of x can change and will change during the selection process and that is why it is termed a variable while the value on y cannot change at any point in time hence, constant.

Now, we have

let x = 10; 
const y = 3; 

Let's leave that for now.

In order to get all possible combinations for the 3 members of a team out of 10 possible members, we have to create a set of new sets or subsets if you'd like and each subset will contain each member of the original set and two other members of the set until we have a new set featuring each member of the set at least once.

The total number of elements in that new set is the total number of possible combinations for that selection process

Again, that's not very exact and that is because according to MathsIsFun combinations are of two types

  • Combination with repetition
  • Combination without repetition

Now back to our function, let's name it getAllCombinations so that we have in our code editor

async function getAllCombinations (a, b) {} 

In the function above a is the variable value which is subject to change while b is the constant value that won't change.

In the example a would be 10 and b would be 3.

Combination can be calculated using a simple mathematical formula which is

n!/r!(n−r)! = (n r) 

Where,

n!(pronounced n factorial) is simply the multiplication of a number and every other number less than that number

So the factorial of 3 can be calculated by multiplying 3 x 2 x 1 which is equal to 6.

In order to find the combination of a selection, we just have multiply a number of factorials.

(n r) is a denotation for combination on n and r.

In our formula n is the variable while r is the constant

In order to find the factorial for our values we are going to use a function that I found on Educative

In our code we will add a new function factorial

function factorial(n){ 
  let answer = 1; 
  if (n == 0 || n == 1){ 
    return answer; 
  }else{ 
    for(var i = n; i >= 1; i--){ 
      answer = answer * i; 
    } 
    return answer; 
  }   
} 

We will then utilize this function in our previously created function getAllCombinations.

Applying the formula our function will now look like

function getAllCombinations (a, b) { 
	function factorial(n){ 
	    let answer = 1; 
	    if (n == 0 || n == 1){ 
		return answer; 
   	    }else{ 
		for(var i = n; i >= 1; i--){ 
			answer = answer * i; 
		} 
		return answer; 
	  }   
   } 
 
   const upper = factorial(a) 
  const lower = factorial(b) * factorial(a - b) 
  console.log(upper/lower) 
} 

Now, let's test our example above using our created variables

let x = 10; 
const y = 3; 

If we run the function getAllCombinations(10, 3), in the browser console we should get the value 120 which is the correct answer for a possible combination of 10 and 3.