what is a factorial

Factorial can be used to find out how many ways to order a set of things.

It looks like this n!.

how to find a factorial

The formula to find a factorial is n! = n * (n - 1) * (n - 2) until n is zero.

You can also think of it as the product of all whole numbers of one through n.

Find 7! which is read as seven factorial.

1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040 // 7!

an implementation using JavaScript

a way to implement finding a factorial is to use recursion.

Every time the function call itself it will multiply the current number passed in by what is returned from the additional function calls.

The additional function calls will have the current number subtracted by one passed in.

The function will stop calling repeatedly once the number passed in is one or zero.

JavaScript

function factorial (number) {
  // How the function will exit out of the
  // recursion
  if (number === 1 || number === 0) {
    return 1;
  }
 
  // This will endlessly call the function
  // until the condition is met
  return number * factorial(number-1);
}

passing in 4 as an argument to the factorial function.

JavaScript

// 4 * (4 - 1) * (3 - 1) * (2 - 1) * (1)
// 4 * 3 * 2 * 1 
factorial(4); // 24

conclusion

Factorial can be useful when wanting to know how many different ways something can be ordered or organized.

html
Semantic HTML

Semantic HTML are HTML tags that tell you what type of information each element should contain.

javascript
what is the Fibonacci Sequence

The Fibonacci Sequence is a series of numbers that starts at 0 and 1. The next number in the series is the sum of the last two numbers.