Skip to content

Commit 38156b4

Browse files
committed
recursion
1 parent 8e4fb79 commit 38156b4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
* This repository is provided as more of a follow along structure.
1111

1212
* In class we will be going over the principles listed above and you will be expected to follow along when the instructor asks you to do so.
13+
14+
### Recursion
15+
* recursion is another way to represent a looping program.
16+
* Simliar to a `for loop` or a `while loop` a recursive function will loop until you tell it to stop.
17+
* when solving complex algorithms recursion comes in handy. It is not that great for smaller, less complex algorithms.
18+
* For every recursive function, you'll need to establish what is called a `base case`. A `base case` is a condition that when triggered, will discontinue the call to your function.

recursion.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// to test these problems you can run 'node recursion.js' in your terminal
2+
// Problem 1:
3+
4+
let n = 1;
5+
while (n <= 10) {
6+
console.log('While Loop', n);
7+
n++;
8+
}
9+
10+
// write a recursive - function called countToTen that mimics the while loop above.
11+
12+
// code here
13+
14+
// when you code is ready, un-comment the next line and run the file
15+
// console.log(countToTen());
16+
/* ================ Next Problem ================= */
17+
18+
// Problem 2:
19+
20+
const factorial = n => {
21+
let result = 1;
22+
for (let i = 2; i <= n; i++) {
23+
result *= i;
24+
}
25+
return result;
26+
};
27+
28+
console.log(factorial(5));
29+
30+
// write the above functionin a recursive way.
31+
32+
// when you code is ready, un-comment the next line and run the file
33+
// console.log(recursiveFactorial());

0 commit comments

Comments
 (0)