Skip to content

Commit c690ca8

Browse files
committed
memoization: Code-Beispiel für eine zwichengespeicherte Fibonacci-Funktion in JavaScript:
1 parent 30d3fee commit c690ca8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function fibonacci(n, memo = {}) {
2+
if (n in memo) {
3+
return memo[n];
4+
}
5+
if (n <= 1) {
6+
return n;
7+
}
8+
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
9+
return memo[n];
10+
}
11+
12+
console.log(fibonacci(6)); // Outputs 8

0 commit comments

Comments
 (0)