-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.js
More file actions
62 lines (45 loc) · 1.12 KB
/
recursion.js
File metadata and controls
62 lines (45 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function printUntil(number, max) {
if (number <= max) {
console.log(number);
printUntil(number + 1, max);
}
else
return;
}
printUntil(1, 10);
function sumNtoM(n, m) {
if (n == m)
{
return m; //No hay nada mas que sumar, la suma de n a n = n
}
else
{
//suma n y la suma del resto de los numero n+1 hasta m
return n + sumNtoM(n + 1, m);
}
}
console.log(sumNtoM(1,8000));
// sumNtoM(1,6)
// 1 + sumNtoM(1 + 2, 6)
// 1 + 2 + sumNtoM(1 + 2, 6)
// 1 + 2 + 3 + sumNtoM(1 + 3, 6)
// 1 + 2 + 3 + 4 + sumNtoM(1 + 4, 6)
// 1 + 2 + 3 + 4 + 5 + sumNtoM(1 + 5, 6)
// 1 + 2 + 3 + 4 + 5 + 6 = 21
// sumNtoM(n, m)
// n + sumNtoM(n + 1, m)
// n + (n + 1) + sumNtoM(n + 2, m)
// n + (n + 1) + (n + 2) + sumNtoM(n + 3, m)
//...
// n + (n + 1) + (n + 2) ... + (m - 1) + sumNtoM((m - 1) + 1, m)
const lista = ["This", "should", "become", "a", "sentence"];
function makeString(pos, lista) {
if (pos == lista.length) {
return "";
}
else
{
return lista[pos] + " " + makeString(pos + 1, lista);
}
}
console.log(makeString(0, lista));