File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Roadmap/06 - RECURSIVIDAD/typescript Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ ( ( ) => {
2
+ /*
3
+ * EJERCICIO:
4
+ * Entiende el concepto de recursividad creando una función recursiva que imprima
5
+ * números del 100 al 0.
6
+ */
7
+
8
+ const printNumbers = ( a : number ) : void => {
9
+ if ( a < 0 ) return
10
+ console . log ( a )
11
+ printNumbers ( a - 1 )
12
+ }
13
+
14
+ printNumbers ( 100 )
15
+
16
+ /*
17
+ * DIFICULTAD EXTRA (opcional):
18
+ * Utiliza el concepto de recursividad para:
19
+ * - Calcular el factorial de un número concreto (la función recibe ese número).
20
+ * - Calcular el valor de un elemento concreto (según su posición) en la
21
+ * sucesión de Fibonacci (la función recibe la posición).
22
+ */
23
+
24
+ const factorial = ( n : number ) : number => {
25
+ if ( n < 1 ) return 1
26
+ return n * factorial ( n - 1 )
27
+ }
28
+
29
+ console . log ( factorial ( 8 ) )
30
+
31
+ const fibonacci = ( position : number ) : number => {
32
+ if ( position === 0 ) {
33
+ return 0
34
+ }
35
+ if ( position === 1 ) {
36
+ return 1
37
+ }
38
+ return fibonacci ( position - 1 ) + fibonacci ( position - 2 )
39
+ }
40
+ console . log ( fibonacci ( 7 ) )
41
+ } ) ( )
You can’t perform that action at this time.
0 commit comments