Skip to content

Commit b6dfa0f

Browse files
committed
chapter 08: [Recursion]
1 parent b337fe8 commit b6dfa0f

22 files changed

+402
-3
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"import/prefer-default-export": 0,
2626
"comma-dangle": 0,
2727
"no-underscore-dangle": 0,
28-
"no-param-reassign": 0
28+
"no-param-reassign": 0,
29+
"no-return-assign": 0
2930
}
3031
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Work in Progress.
1717
* 05: [LinkedLists](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter05)
1818
* 06: [Sets](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter06)
1919
* 07: [Dictionaries and Hashes](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter07)
20+
* 08: [Recursion](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter08)
2021

2122
### Third Edition Updates
2223

examples/PacktDataStructuresAlgorithms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="01-IntroRecursion.js"></script>
9+
</body>
10+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
// functions commented below - inifite loop
3+
4+
/*
5+
function recursiveFunction(someParam) {
6+
recursiveFunction(someParam);
7+
}
8+
9+
function recursiveFunction1(someParam) {
10+
recursiveFunction2(someParam);
11+
}
12+
13+
function recursiveFunction2(someParam) {
14+
recursiveFunction1(someParam);
15+
}
16+
*/
17+
18+
function understandRecursion(doIunderstandRecursion) {
19+
const recursionAnswer = confirm('Do you understand recursion?'); // function logic
20+
if (recursionAnswer === true) { // base case or stop point
21+
return true;
22+
}
23+
understandRecursion(recursionAnswer); // recursive call
24+
}
25+
26+
understandRecursion(false);

examples/chapter08/02-Factorial.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="02-Factorial.js"></script>
9+
</body>
10+
</html>

examples/chapter08/02-Factorial.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @ts-check
2+
3+
function factorialIterative(number) {
4+
if (number < 0) {
5+
return undefined;
6+
}
7+
let total = 1;
8+
for (let n = number; n > 1; n--) {
9+
total = total * n;
10+
}
11+
return total;
12+
}
13+
14+
console.log('factorialIterative(5): ', factorialIterative(5));
15+
console.log('factorialIterative(3): ', factorialIterative(3));
16+
17+
function factorial(n) {
18+
// console.trace();
19+
if (n === 1 || n === 0) {
20+
return 1;
21+
}
22+
return n * factorial(n - 1);
23+
}
24+
25+
console.log('factorial(5): ', factorial(5));
26+
console.log('factorial(3): ', factorial(3));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="03-JSCallStack.js"></script>
9+
</body>
10+
</html>

examples/chapter08/03-JSCallStack.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let i = 0;
2+
function recursiveFn() {
3+
i++;
4+
recursiveFn();
5+
}
6+
7+
try {
8+
recursiveFn();
9+
} catch (ex) {
10+
console.log('i = ' + i + ' error: ' + ex);
11+
}

examples/chapter08/04-Fibonacci.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="04-Fibonacci.js"></script>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)