-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathfactorial.js
37 lines (30 loc) · 932 Bytes
/
factorial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Write two functions that finds the factorial of any number. One should use recursive, the other should just use a for loop
function findFactorialRecursive(number) {
//code here
return answer;
}
function findFactorialIterative(number) {
//code here
return answer;
}
//ANSWER:
function findFactorialIterative(number) {
let answer = 1;
// you actually no longer need the if statement because of the for loop
// if (number === 2) {
// answer = 2;
// }
for (let i = 2; i <= number; i++) {
answer = answer * i;
}
return answer;
}
function findFactorialRecursive(number) {
if(number === 2) {
return 2;
}
return number * findFactorialRecursive(number - 1)
}
findFactorialIterative(5);
findFactorialRecursive(5);
//If you want, try to add a base case condition for the recursive solution if the parameter given is less than 2