-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
197 lines (138 loc) · 3.81 KB
/
practice.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* ciclos anidados */
var miArreglo = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
for (var i = 0; i < miArreglo.length; i++) {
var arregloAnidado = miArreglo[i]; // Arreglo
for (var j = 0; j < arregloAnidado.length; j++) {
console.log(arregloAnidado[j]); //Elemento
}
}
/* Encontrar numero par */
function detectarPares (arreglo) {
var total = 0;
for (var i = 0; i < arreglo.length; i++) {
if (arreglo[i] % 2 == 0) {
total++;
}
}
return total;
}
console.log(detectarPares([5, 3, 1, 2, 4, 8]));
/* Ejercisio de Javascript */
var contactos =[
{
"nombre": "Nora",
"apellido": "Nav",
"numero": "0543236543",
"gustos": ["Pizza", "Programacion"]
},
{
"nombre": "Harry",
"apellido": "Potter",
"numero": "0999236543",
"gustos": ["hogwarts", "Magia"]
},
{
"nombre": "Sherlock",
"apellido": "Holmes",
"numero": "05432345856",
"gustos": ["Casos Interesantes", "Violin"]
}
]
function buscarPerfil(nombre, propiedad) {
for (var i = 0; i < contactos.length; i++) {
if (contactos[i].nombre == nombre){
return contactos[i][propiedad] || "La propiedad no esxiste.";
}
}
return "El contacto no esta en la lista.";
}
console.log(buscarPerfil("Nora", "gustos"));
console.log(buscarPerfil("Harry", "apellido"));
function generarEnteroAleatorio(limiteSuperior) {
// Generar un entero aleatorio entre 0 y el limite
// superior (sin incluirlo)
return Math.floor( Math.random() * limiteSuperior)
}
for (var i = 0; i < 15; i++) {
console.log(generarEnteroAleatorio(5));
}
function retornarMinimo(x, y) {
return x < y ? x : y;
}
function compararNumeros(a, b) {
return a == b ? "a y b son iguales"
: a > b ? "a es mayor que b"
: "b es mayor que a";
}
console.log(compararNumeros(11, 27));
function calcularAreaCirculo(radio) {
const PI = 5.14;
if (radio < 0) {
return undefined;
}
return PI * (radio ** 2);
}
console.log(calcularAreaCirculo(10));
/* extraer informacion con la propiedad de desestructuracion */
const usuario = {
johnDoe: {
edad: 27,
correo: "[email protected]"
}
};
const {johnDoe: {edad: edadUsuario, correo: correoUsuario}} = usuario;
console.log(edad);
console.log(correo);
const PRONOSTICO_LOCAL = {
"ayer": {
minima: 61,
maxima: 75
},
"hoy": {
minima: 64,
maxima: 7
},
"mañana": {
minima: 68,
maxima: 80
}
};
// const minimaHoy = PRONOSTICO_LOCAL.hoy.minima;
// const maximaHoy = PRONOSTICO_LOCAL.hoy.maxima;
// console.log(minimaHoy);
// console.log(maximaHoy);
/* podemos asignarle un nombre a la variable para llamarla en la funcion que estamos haciendo */
const {hoy: {minima: minimaHoy}} = PRONOSTICO_LOCAL;
console.log(minimaHoy);
/* sintaxis de desestructuracion para intercambiar valores */
var a = 8;
var b = 6;
[b, a] = [a, b];
console.log("a: " + a);
console.log("b: " + b);
/* tambien podemos remover elementos de un arreglo con la propiedad brech ... */
const arregloInicial = [1, 2, 3, 4, 5, 6, 7, 8];
function removerTresPrimerosElementos(arreglo) {
const [ , , , ...nuevoArreglo] = arreglo;
return nuevoArreglo;
}
const arregloFinal = removerTresPrimerosElementos(arregloInicial);
console.log(arregloFinal);
/* sintaxis de desestructuracion, escribimos las propiedades que nesesitamos
dentro de los corchetes para llamarlas y va asignar cada propiedad a su variable
correspondiente. */
const estadisticas = {
max: 56.78,
desviacionEstandar: 4.34,
mediana: 34.54,
moda: 23.87,
min: -0.75,
promedio: 35.85
};
const mitad = ({max, min}) => (max + min) / 2.0;
console.log(mitad(estadisticas));
var persona = {
nombre = "Leopardo",
edad = 10
};
const saludo = `!hola!`