This commit is contained in:
Philippe Torrel
2026-09-22 14:45:24 +02:00
parent 1f2db53534
commit 04831eb9c1
14 changed files with 568 additions and 0 deletions
@@ -0,0 +1,40 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rekursion Beispiel: Fakultät errechnen</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<h1>Rekursion Beispiel: Fakultät errechnen</h1>
<div class="output alert alert-secondary my-3"></div>
</div>
</main>
<script>
'use strict';
const outputEl = document.querySelector('.output');
const calcFactorial = (n) => {
// Base case: if n is 0, return 1
if (n === 0) {
return 1;
}
// Recursive case: n * factorial of (n - 1)
return n * calcFactorial(n - 1); // 5 * (calcFactorial(4)) -> 4 * (calcFactorial(4 - 1)) -> 3 * (calcFactorial(3 - 1)) -> 2 * (calcFactorial(2 - 1)) -> 1 * (calcFactorial(1 - 1)) -> 1
};
// TODO: Fakultät mit while Schleife
const calcFactorialWithWhile = () => {};
// Test cases
console.log(calcFactorial(5)); // => 120
console.log(calcFactorial(3)); // => 6
console.log(calcFactorial(0)); // => 1
outputEl.textContent = `${calcFactorial(5)}, ${calcFactorial(3)}, ${calcFactorial(0)} `;
</script>
</body>
</html>
@@ -0,0 +1,43 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rekursion Beispiel: Fibonacci-Reihenfolge errechnen</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<h1>Rekursion Beispiel: Fibonacci-Reihenfolge errechnen</h1>
</div>
</main>
<script>
'use strict';
const calcFibonacci = (n) => {
// Base cases
if (n === 0) {
return 0;
}
if (n === 1) {
return 1;
}
// Recursive case
return calcFibonacci(n - 1) + calcFibonacci(n - 2);
};
// Test cases
console.log(calcFibonacci(0)); // => 0
console.log(calcFibonacci(1)); // => 1
console.log(calcFibonacci(2)); // => 1
console.log(calcFibonacci(3)); // => 2
console.log(calcFibonacci(4)); // => 3
console.log(calcFibonacci(5)); // => 5
console.log(calcFibonacci(6)); // => 8
console.log(calcFibonacci(100)); // => 55
</script>
</body>
</html>
@@ -0,0 +1,60 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rekursion Beispiel: Summe eines Array errechnen</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<h1>Rekursion Beispiel: Summe eines Array errechnen</h1>
</div>
</main>
<script>
'use strict';
const sumArray = (ar = []) => {
// Base case: wenn Array leer ist, return 0
if (ar.length === 0) {
return 0;
}
return ar[0] + sumArray(ar.slice(1));
// => 1 + (fn([2,3,4,5])) -> 2 + (fn([3,4,5])) -> 3 + (fn([4,5])) -> 4 + (fn([5])) + 5 (fn([])) + 0
};
const sumArrayReduce = (ar = []) => {
return ar.reduce((a, b) => a + b, 0);
};
const sumArrayWhile = (ar = []) => {
let i = ar.length;
let sum = 0;
if (i === 0) {
return 0;
}
while (--i >= 0) {
sum += ar[i];
}
return sum;
};
// Test Cases:
// https://jsben.ch/
console.time('rekursion');
console.log(sumArray([1, 2, 3, 4, 5])); // => 15
console.timeEnd('rekursion');
console.time('reduce');
console.log(sumArrayReduce([1, 2, 3, 4, 5])); // => 15
console.timeEnd('reduce');
console.time('while');
console.log(sumArrayWhile([1, 2, 3, 4, 5])); // => 15
console.timeEnd('while');
</script>
</body>
</html>