-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperaciones_array.js
More file actions
92 lines (60 loc) · 1.6 KB
/
operaciones_array.js
File metadata and controls
92 lines (60 loc) · 1.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Los tres puntos dentro de la declaración indica que es un array ...
// COSAS QUE HACEN QUE UN DESARROLLADOR SE DESTAQUE SOBRE OTROS.
// Primera forma
function suma(...numeros){
console.log(numeros)
for (let i = 0; i < numeros.length; i++){
acum += numeros[i]
}
return acum
}
// Segunda forma para sumar con reduce.
function suma2(...numeros){
return numeros.reduce(function (acum, numero){
acum += numero
return acum
}, 0)
console.log()
}
suma2(4, 8, 12, 854, 7)
// Primera forma de hacer dobles
function dobles(...numeros){
for (let i = 0; i < numeros.length; i++){
resultado.push(numeros[i] * 2)
}
return resultado
}
// Segunda forma de hacer dobles, usando map.
// map es una función que se ejcuta por cada numero de los elementos
function dobles(...numeros){
return numeros.map(numero => return numero * 2
/*return numeros.map(function(numero){
return numero * 2
})*/
}
// mas estetica que la de arriba:
const dobles2 = (...numeros) => numeros.map(numero => numero * 2)
dobles2(4, 8, 8954, 7, 9)
// Utilizar filter:
function pares(...numeros){
const resultad = []
const length = numeros.length
for (let i = 0; i < length; i++){
if (numero % 2 == 0){
resultado.push(numero)
}
}
return resultado
}
pares(3, 50, 10)
// Mejor forma de usar la función de arriba:
function pares2(...numeros){
return numeros.filter(function(numero){
return numero % 2 == 0
})
// usando arrow function
//return numeros.filter(numero => numero % 2 == 0)
}
pares2(3, 50, 10)
// filter usando arrow functions
const pares3 = (...numeros) => numeros.filter(numero => numero % 2 == 0)