You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-4Lines changed: 13 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,29 @@
1
1
# Recursion
2
-
---
2
+
3
3
##### **What is this?**
4
4
This is a repository of toy problems to be solved using recursion and JavaScript. While the concept of recursion may not be difficult to grasp, the only way to improve at thinking recursively is by practice. If you need practice, then maybe this repo is for you.
5
5
6
6
##### **A few guidelines:**
7
7
- Please refrain from sharing solutions. As crazy as it sounds, giving someone the answer doesn't help them. Instead, give them a question that encourages them to think differently.
8
+
8
9
> **Q:** Why does my function keep exceeding the call stack?
9
10
> **A:** What's your base case?
11
+
10
12
- Don't be afraid to pseudocode your algorithm before writing actual code.
13
+
11
14
> Pseudocode helps you focus on the algorithm instead of getting distracted by syntax.
15
+
12
16
- This repo requires each function call itself recursively and pays no attention to whether inner recursive functions defined and called.
17
+
13
18
> While both are valid uses of recursion, there are important lessons to learn by following the method this repo enforces. Defining inner functions and calling them recursively relies on side effects, while following the more pure approach requires an understanding of how values are passed through the call stack.
19
+
14
20
- This repo restricts expanding the number of parameters a function accepts.
21
+
15
22
> Expanding the number of parameters is a valid approach, but has been restricted here to emphasize certain lessons while learning recursion.
23
+
16
24
- An attempt was made to order prompts by difficulty, but they don't have to be solved in any particular order.
17
25
- Feel free to make pull requests or open issues regarding bugs or suggestions.
18
-
-`Watch`, `Star`, and `Fork` this repo. You know you want to.
26
+
-**`Watch`**, **`Star`**, and **`Fork`** this repo. You know you want to.
19
27
20
28
##### **How to use this repo:**
21
29
1. Fork this repo and clone it to your local machine
@@ -29,8 +37,8 @@ This is a repository of toy problems to be solved using recursion and JavaScript
29
37
> Recursion is when a function calls itself until it doesn't. --not helpful person
30
38
31
39
Is it a true definition? Mostly. Recursion is when a function calls itself. A recursive function can call itself forever, but that's generally not preferred. It's often a good idea to include a condition in the function definition that allows it to stop calling itself. This condition is referred to as a **_base_** case. As a general rule, recursion shouldn't be utilized without an accompanying base case unless an infinite operation is desired. This leaves us with two fundamental conditions every recursive function should include:
32
-
- A `base` case
33
-
- A `recursive` case
40
+
- A **`base`** case
41
+
- A **`recursive`** case
34
42
35
43
What does this all mean? Let's consider a silly example:
36
44
```sh
@@ -45,6 +53,7 @@ function stepsToZero(n) {
45
53
}
46
54
```
47
55
This functiondoesn'tdo anything meaningful, but hopefully it demonstrates the fundamental idea behind recursion. Simply put, recursion provides us a looping or repeating mechanism. It repeats an operation until a `base` condition is met. Let's step through an invocation of the above function to see how it evaluates.
56
+
48
57
1. Invoke `stepsToZero(n)` where `n` is the number `2`
49
58
2. Is 2 zero?
50
59
3. No, print message to console that 2 is not zero
0 commit comments