Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
To find the factorial of a number multiply the number by every whole number less than it, down to 1.
The factorial of 0 is defined as 1.
0! = 1
0! = 1
1! = 1
2! = 2 × 1 = 2
3! = 3 × 2 × 1 = 6
4! = 4 × 3 × 2 × 1 = 24
5! = 5 × 4 × 3 × 2 × 1 = 120
6! = 6 × 5 × 4 × 3 × 2 × 1 = 720
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
Factorials are useful for counting the number of ways to arrange, combine, or select distinct items.
For example, how many different ways can n things be arranged?
Example:
How many ways can three books be arranged in a row?
3! = 3 × 2 × 1 = 6
There are 6 ways to arrange three books in a row.
3! = 6
Find the factorial of a given number using recursion
Recursion The act of a function calling itself.
Recursion is used to solve problems that contain smaller sub-problems.
A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).
Use recursion to find the factorial of 5.
let x = 5;
function factorial(num) {
if (num > 1) { → Recursive call
return num * factorial(num - 1);
}
else { → Base case
return 1;
};
}
call function
factorial(x); returns ↴
120 factorial of 5 → 120
Initialize a variable to hold given number n of the factorial.
const number = n; → user input
Define a function factorial that calculates the factorial of a given number.
function factorial(num) {}
The function takes a non-negative number as input, num and returns its factorial.
The function checks for two Base cases:
Base case The condition under which the recursion stops.
If num equals 0 or 1
Factorial is not defined for negative numbers ↴
if (num < 0) return -1
Return -1 (indicating an error) and end execution of function.
Base case The condition under which the recursion stops.
Check if num is 0 ↴
else if (num == 0) return 1
Return 1 and end execution of function.
Recursive case num is greater than 1
factorial function calls itself with decremented values until it reaches the base case.
If num is not negative or 0
call the function recursively ↴
return num * factorial( num - 1)
Function recursively calls itself, multiplying the current number by the factorial of the number minus one.
This recursive call continues until it reaches one of the base cases.
Call the function with ↴
factorial(number);
Find the factorial of 5
num = 5;
function factorial(num)
factorial(5) 5 × factorial(4)
factorial(4) 4 × factorial(3)
factorial(3) 3 × factorial(2)
factorial(2) 2 × factorial(1)
factorial(1) 1 Base case
The function stacks calls, until it reaches the base case, then unwinds the stack to produce the result ↴
factorial(2) 2 × 1 → 2
factorial(3) 3 × 2 → 6
factorial(4) 4 × 6 → 24
factorial(5) 5 × 24 → 120 result
5! = 120
Find the factorial of 5
const number = 5;
function factorial(num) {
if (num < 0) return -1;
else if (num == 0) return 1;
else {
return num * factorial( num - 1);
}
}
call function
factorial(number); returns ↴
120
Alternative ↴
Find the factorial of 5
const number2 = 5;
function factorial2(num) {
if (num === 0 || num === 1) {
return 1;
}
return num * factorial2(num - 1);
}
call function
factorial2(number2); returns ↴
120
Alternative using a ternary operator ↴
const number3 = 5;
function factorial3(num) {
return num === 0 ? 1 : num * factorial3(num - 1);
}
call function
factorial3(number3); returns ↴
120