Skip to content

Commit 4fc9dee

Browse files
committed
fibo
1 parent 0201df1 commit 4fc9dee

File tree

2 files changed

+110
-41
lines changed

2 files changed

+110
-41
lines changed

Diff for: Part_1_Basics/5_final_exercise.js

+31-41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Welcome to the 3nd exercise sheet of Programming Fundamentals in JavaScript!
33
///////////////////////////////////////////////////////////////////////////////
44

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.
67

78
// EXERCISE 1. Fibonacci.
89
/////////////////////////
@@ -12,59 +13,48 @@
1213
// You can learn more here: https://en.wikipedia.org/wiki/Fibonacci_number
1314

1415
// 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.
2618

27-
fibonacci(10);
2819

2920
// 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);
3938
}
39+
resultOfRecursion = recursive(5);
40+
console.log(resultOfRecursion);
4041

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.
4946

50-
fibonacciRecursive(10);
47+
// Now write the fibonacci recursive function.
48+
// Hint: it takes just 2 lines (but you could make it one).
5149

52-
counter = 0;
53-
myInterval = setInterval(function() {
54-
console.log(new Date());
55-
if (++counter > 10) clearInterval(myInterval);
56-
}, 2000);
5750

5851

59-
// FUN EXERCISE
60-
52+
// FUN EXERCISE!
53+
////////////////
6154
// You have done everything! You are a Jenius (shorthand for JavaScript Genius)!
6255
// Why don't you relax and have some fun programming a JS robot?
6356
// Check out: https://lab.reaal.me/jsrobot/
6457

6558

66-
67-
68-
6959
// Great work! You finish the third exercise sheet!
7060
// Stop patting yourself, enough.

Diff for: Part_1_Basics/solutions/5_final_exercise.js

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

0 commit comments

Comments
 (0)