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 a while loop
while loop repeatedly executes a block of code as long as a specified condition evaluates to true.
while (condition) {
// execute code as long as condition is true
}
let x = 0; → counter
while (x < 4) {
console.log(x);
x++;
}
Initialize a counter variable x outside of the loop.
Condition x < 4 is checked before each iteration.
The loop will continue to run as long as x is less than 4
The loop repeatedly executes a block of code 4 times, from 0 to 3
For each iteration of the loop, the current value of x is printed to the console.
After each iteration, x is incremented by 1 x++
When x reaches 4 the condition evaluates to false, terminating the loop.
0
1
2
3 → printed to console
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.
Initialize a variable with the value of num
let result = num result holds the accumulated result
Base case
If num equals 0 or 1 return 1
if (num === 0 || num === 1) return 1
return 1 and end execution of the function.
Loop while num is greater than 1
while (num > 1) {}
Decrement num by 1 at each iteration.
num--
Multiply result by the decremented num
result *= num result = result * num
Return computed factorial value.
return result
Call the function with ↴
factorial(number);
Find the factorial of 5
num = 5;
function factorial(num)
The function checks if num is 0 or 1
If it is, it returns 1 and ends execution of the function.
If num is greater than 1 the function proceeds.
while (num > 1) {}
result *= num; result = result * num
First iteration of loop ↴
result → 5
num → 5
num-- num = num - 1 → 4
num Iteration ↴
4 result = 5 × 4 → 20
3 result = 20 × 3 → 60
2 result = 60 × 2 → 120
1 Loop ends and the calculated factorial of 5 is returned ↴
5! = 120
Find the factorial of 5
const number = 5;
function factorial(num) {
let result = num;
if (num === 0 || num === 1) return 1;
while (num > 1) {
num--;
result *= num;
}
return result;
}
call function
factorial(number); returns ↴
120