Skip to content

Commit e75e24a

Browse files
committed
factorialize a number
1 parent e48ab5e commit e75e24a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
1313
- [Basic Algorithm Scripting](#basic-algorithm-scripting)
1414
- [1. Convert Celsius to Fahrenheit](#1-convert-celsius-to-fahrenheit)
1515
- [2. Reverse a String](#2-reverse-a-string)
16+
- [3. Factorialize a Number](#3-factorialize-a-number)
1617

1718
## Basic Algorithm Scripting
1819

@@ -72,4 +73,49 @@ function reverseString(str) {
7273
7374
reverseString("hello");
7475
76+
```
77+
78+
### 3. Factorialize a Number
79+
80+
### Difficulty: Beginner
81+
82+
Return the factorial of the provided integer.
83+
84+
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
85+
86+
Factorials are often represented with the shorthand notation n!
87+
88+
For example: 5! = 1 _ 2 _ 3 _ 4 _ 5 = 120
89+
90+
Only integers greater than or equal to zero will be supplied to the function.
91+
92+
---
93+
94+
### Solution 1
95+
96+
```
97+
function factorialize(num) {
98+
if (num === 0) {
99+
return 1;
100+
} else return num * factorialize(num - 1);
101+
}
102+
103+
factorialize(5);
104+
105+
```
106+
107+
### Solution 2
108+
109+
```
110+
function factorialize(num) {
111+
let factorializedNumber = 1;
112+
for (let i = 2; i <= num; i++) {
113+
factorializedNumber *= i;
114+
}
115+
return factorializedNumber;
116+
}
117+
118+
factorialize(5);
119+
120+
75121
```

0 commit comments

Comments
 (0)