Skip to content

Commit 287d6d4

Browse files
committed
feat(js): add solution and documentation for easy challenge 3 - Fibonacci sequence generator
1 parent e275305 commit 287d6d4

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Challenge Description and Solution
2+
3+
## English Version
4+
5+
### Challenge Description
6+
Create a generator that returns the Fibonacci sequence up to n terms. You can explore iterative and recursive approaches to solve the problem.
7+
8+
### Code Explanation
9+
The code presents two generator functions for the Fibonacci sequence:
10+
11+
- `fibonacciIterative(n)`: Uses a `for` loop to generate the first n terms of the Fibonacci sequence iteratively. It initializes two variables `a` and `b` with 0 and 1 respectively, and in each iteration yields the current value of `a` and updates `a` and `b`.
12+
13+
- `fibonacciRecursive(n, a=0, b=1)`: Generates the Fibonacci sequence recursively. If `n` is 0, it ends the recursion. Otherwise, it yields the current value `a` and recursively calls itself with `n-1`, `b`, and `a+b`.
14+
15+
### Relevant Code Snippet
16+
17+
```javascript
18+
function* fibonacciIterative(n) {
19+
let a = 0, b = 1;
20+
for (let i = 0; i < n; i++) {
21+
yield a;
22+
[a, b] = [b, a + b];
23+
}
24+
}
25+
26+
function* fibonacciRecursive(n, a = 0, b = 1) {
27+
if (n === 0) return;
28+
yield a;
29+
yield* fibonacciRecursive(n - 1, b, a + b);
30+
}
31+
```
32+
33+
### Example Usage
34+
35+
```javascript
36+
const n = 10;
37+
console.log("Iterative Fibonacci sequence:");
38+
for (const num of fibonacciIterative(n)) {
39+
process.stdout.write(num + " ");
40+
}
41+
console.log("\nRecursive Fibonacci sequence:");
42+
for (const num of fibonacciRecursive(n)) {
43+
process.stdout.write(num + " ");
44+
}
45+
console.log();
46+
```
47+
48+
---
49+
50+
## Versión en Español
51+
52+
### Descripción del Reto
53+
Crea un generador que devuelva la secuencia de Fibonacci hasta n términos. Puedes explorar enfoques iterativos y recursivos para resolver el problema.
54+
55+
### Explicación del Código
56+
El código presenta dos funciones generadoras para la secuencia de Fibonacci:
57+
58+
- `fibonacciIterative(n)`: Utiliza un bucle `for` para generar los primeros n términos de la secuencia de Fibonacci de manera iterativa. Inicializa dos variables `a` y `b` con 0 y 1 respectivamente, y en cada iteración produce el valor actual de `a` y actualiza `a` y `b`.
59+
60+
- `fibonacciRecursive(n, a=0, b=1)`: Genera la secuencia de Fibonacci de forma recursiva. Si `n` es 0, termina la recursión. De lo contrario, produce el valor actual `a` y llama recursivamente a sí misma con `n-1`, `b` y `a+b`.
61+
62+
### Fragmento de Código Relevante
63+
64+
```javascript
65+
function* fibonacciIterative(n) {
66+
let a = 0, b = 1;
67+
for (let i = 0; i < n; i++) {
68+
yield a;
69+
[a, b] = [b, a + b];
70+
}
71+
}
72+
73+
function* fibonacciRecursive(n, a = 0, b = 1) {
74+
if (n === 0) return;
75+
yield a;
76+
yield* fibonacciRecursive(n - 1, b, a + b);
77+
}
78+
```
79+
80+
### Ejemplo de Uso
81+
82+
```javascript
83+
const n = 10;
84+
console.log("Secuencia de Fibonacci iterativa:");
85+
for (const num of fibonacciIterative(n)) {
86+
process.stdout.write(num + " ");
87+
}
88+
console.log("\nSecuencia de Fibonacci recursiva:");
89+
for (const num of fibonacciRecursive(n)) {
90+
process.stdout.write(num + " ");
91+
}
92+
console.log();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Challenge: Create a generator that returns the Fibonacci sequence up to n terms.
2+
// You can explore iterative and recursive approaches to solve the problem.
3+
4+
// Iterative generator function
5+
function* fibonacciIterative(n) {
6+
let a = 0, b = 1;
7+
for (let i = 0; i < n; i++) {
8+
yield a;
9+
[a, b] = [b, a + b];
10+
}
11+
}
12+
13+
// Recursive generator function
14+
function* fibonacciRecursive(n, a = 0, b = 1) {
15+
if (n === 0) return;
16+
yield a;
17+
yield* fibonacciRecursive(n - 1, b, a + b);
18+
}
19+
20+
// Example usage
21+
const n = 10;
22+
console.log("Iterative Fibonacci sequence:");
23+
for (const num of fibonacciIterative(n)) {
24+
process.stdout.write(num + " ");
25+
}
26+
console.log("\nRecursive Fibonacci sequence:");
27+
for (const num of fibonacciRecursive(n)) {
28+
process.stdout.write(num + " ");
29+
}
30+
console.log();

0 commit comments

Comments
 (0)