|
2 | 2 | // Welcome to the 3nd exercise sheet of Programming Fundamentals in JavaScript!
|
3 | 3 | ///////////////////////////////////////////////////////////////////////////////
|
4 | 4 |
|
5 |
| -// Here we try to put things together |
| 5 | +// Here we try to put things together and we learn about a new thing: |
| 6 | +// recursion. |
6 | 7 |
|
7 | 8 | // EXERCISE 1. Fibonacci.
|
8 | 9 | /////////////////////////
|
|
12 | 13 | // You can learn more here: https://en.wikipedia.org/wiki/Fibonacci_number
|
13 | 14 |
|
14 | 15 | // a. Implement a function that computes the fibonacci number for
|
15 |
| -// any input number. |
16 |
| -// For instance, if input is 10, the result is 55. |
17 |
| - |
18 |
| -function fibonacci(n) { |
19 |
| - let fibo = [ 0, 1 ]; |
20 |
| - if (n < 2) return fibo[n]; |
21 |
| - for (let i=2; i <= n; i++) { |
22 |
| - fibo[i] = fibo[i-1] + fibo[i-2]; |
23 |
| - } |
24 |
| - return fibo[n]; |
25 |
| -} |
| 16 | +// any input number. For instance, if input is 10, the result is 55. |
| 17 | +// Motivational Hint: This exercise is often asked at job interviews. |
26 | 18 |
|
27 |
| -fibonacci(10); |
28 | 19 |
|
29 | 20 | // EXERCISE 2. Bonus. Recursive Fibonacci.
|
30 |
| - |
31 |
| -// Recursion is the |
32 |
| - |
33 |
| - |
34 |
| -function fibonacciRecursive(n, counter) { |
35 |
| - counter = counter || 0; |
36 |
| - if (n > 1) fibo = fibonacciRecursive(n-1, n); |
37 |
| - else fibo = n; |
38 |
| - return fibo + counter; |
| 21 | +////////////////////////////////////////// |
| 22 | + |
| 23 | +// Recursion is a computer programming technique in which a function invokes |
| 24 | +// itslef inside its body. It's like playing with fire. It can be luring and |
| 25 | +// and elegant (specially if you work in a circus), but also very dangerous. |
| 26 | +// Your code could get stuck in an infinite loop of function calls which |
| 27 | +// ultimately will crash your program. |
| 28 | + |
| 29 | +// Here is an example of simple, but not so useful, recursive function |
| 30 | +// that counts until 5 and then returns |
| 31 | + |
| 32 | +function recursive(number = 5, stopCondition = 0) { |
| 33 | + // Stopping-rule. |
| 34 | + if (stopCondition === number) return stopCondition; |
| 35 | + console.log('Counting...' + (stopCondition+1)); |
| 36 | + // Self-invocation with increment of the stopping condition. |
| 37 | + return recursive(number, ++stopCondition); |
39 | 38 | }
|
| 39 | +resultOfRecursion = recursive(5); |
| 40 | +console.log(resultOfRecursion); |
40 | 41 |
|
41 |
| -fiboArray = [0, 1]; |
42 |
| -function fibonacciRecursive(n) { |
43 |
| - if (n > 2) { |
44 |
| - fiboArray[n] = fibonacciRecursive(n-1); |
45 |
| - fiboArray[n+1] = fibonacciRecursive(n) + |
46 |
| - fibonacciRecursive(n-1); |
47 |
| - } |
48 |
| -} |
| 42 | +// Cool! But what happens if you change ++stopCondition into stopCondition++? |
| 43 | +// You can try it, but make sure you save all your open files, because |
| 44 | +// you will get stuck in an infinite loop. |
| 45 | +// Small changes can cause big problems. |
49 | 46 |
|
50 |
| -fibonacciRecursive(10); |
| 47 | +// Now write the fibonacci recursive function. |
| 48 | +// Hint: it takes just 2 lines (but you could make it one). |
51 | 49 |
|
52 |
| -counter = 0; |
53 |
| -myInterval = setInterval(function() { |
54 |
| - console.log(new Date()); |
55 |
| - if (++counter > 10) clearInterval(myInterval); |
56 |
| -}, 2000); |
57 | 50 |
|
58 | 51 |
|
59 |
| -// FUN EXERCISE |
60 |
| - |
| 52 | +// FUN EXERCISE! |
| 53 | +//////////////// |
61 | 54 | // You have done everything! You are a Jenius (shorthand for JavaScript Genius)!
|
62 | 55 | // Why don't you relax and have some fun programming a JS robot?
|
63 | 56 | // Check out: https://lab.reaal.me/jsrobot/
|
64 | 57 |
|
65 | 58 |
|
66 |
| - |
67 |
| - |
68 |
| - |
69 | 59 | // Great work! You finish the third exercise sheet!
|
70 | 60 | // Stop patting yourself, enough.
|
0 commit comments