-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-intro.js
327 lines (223 loc) · 6.23 KB
/
1-intro.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* -----------------------------1.- Introducción ------------------------------------*/
/* ------------------ 1.1 Variables ---------------------*/
var nombre = "Carlos";
var entero = 19
print(nombre)
print(entero)
console.log("my name is"+nombre)
console.log(nombre + nombre)
console.log(typeof(nombre)) /// tipo de variable ///
/* data types */
/* boolean */
var data = false;
if (data) {
console.log("es buleano")
}else{
console.log("no es buleano")
}
/* null */
var n = null;
console.log(n*32)
console.log(n)
/* undefined */
var a;
console.log(a + 2);
/* number */
var num =3.6;
var ber=6.3;
console.log(num+ber)
/* string */
var name="beau";
var name2="beau";
console.log(name+name2)
console.log(name[0])
console.log(name.length)
console.log(name[name.length -2])
/* object */
var myCar = new Object();
myCar.make = "Ford";
myCar.model = "Mustang";
console.log(myCar.make);
/*--------------------1.2.- String Methods ---------------------------------*/
var stringOne = "freeCodeCamp is the best place to learn"
var stringTwo = "frontend and backend development"
// charAt() devuelve la posición del string especificado
console.log(stringOne.charAt(1))
// charCodeAt() retorna unicode del caracter
console.log(stringOne.charCodeAt(a))
// concat() concatena strings
console.log(stringOne.concat(stringTwo))
// fromCharCode() de unicode a character
console.log(String.fromCharCode(114))
// indexOf()
console.log(stringTwo.indexOf("end"))
// lastIndexOf() provee
console.log(stringTwo.lastIndexOf("end"))
// match()
console.log(stringTwo.match(/end/g))
// replace()
console.log(stringTwo.replace(/end/g,"END"))
// search() retorna la posición
console.log(stringTwo.search('end'))
// slice() retorna caracteres entre las posiciones indicadas
console.log(stringTwo.slice(2,6))
// split() retorna lista con separacion por " lo que haya aqui "
console.log(stringOne.split(" "))
// substr()
console.log(stringTwo.substr(2,4))
// toLowerCase() and toUpperCase
console.log(stringTwo.toLowerCase())
console.log(stringTwo.toUpperCase())
// trim() remueve los espacios de los extremos
var stringThree = " hola "
console.log(stringThree.trim())
/* 1.3 --------------------------- Functions ------------------------ */
function square(number1,number2){
return number1*number2;
}
/* 1.4 -------------------- compariso operator & If, Else ---------- */
var age = 10;
if(age >= 18){
console.log("You are an adult");
} else if (age < 2){
console.log("You are a baby!");
} else if (age < 18){
console.log("You are a child");
};
// == abstract equality
// === strict equality
console.log(3 == '3')
console.log(3 === '3')
console.log(true == '1')
console.log(true === '1')
console.log(true == 1)
console.log(true === 1)
/* 1.5 ---------------- Null vs Undefined ------------------ */
var test
console.log(test)
test = null
console.log(test)
console.log(typeof null)
console.log(typeof undefined)
console.log(null === undefined)
console.log(null == undefined)
console.log(null === null)
console.log(null == null)
console.log(!null)
console.log(!!null)
console.log(1 + null)
console.log(1 + undefined)
/* 1.6 ----------------- logical operators -------------- */
// AND: &&, OR:
if (num > 5 || num <10) {
console.log("Unicycle!")
}
if (num > 5 && num <10) {
console.log("Unicycle!")
}
/* 1.7 ----------------- JS Nuggets: Ternary Operator ------------- */
var age = 19;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a kid")
};
console.log((age >= 18) ? "You are and adult" : "You are a kid");
var stop;
age > 18 ? (
console.log("Ok, you can go."),
stop = false
) : (
console.log("Sorry, you are much too young!"),
stop = true
);
var firstCheck = false,
secondCheck = false,
access = firstCheck ? "Acces denied" : secondCheck ? "Acces denied" : "Acces granted"
console.log(access);
/* 1.8 ------------- array basics ----------------------- */
var sandwich = ["peanut butter","jelly","bread"];
var teams = [["Bulls",23],["White Sox",45]];
console.log(sandwich[1])
console.log(teams[0][0])
// especie de funcion/ciclo para el array
sandwich.forEach(function(element){
console.log(element);
});
/* 1.9 --------------------- Array Methods -------------------- */
var arr = ["a","b","c"];
console.log(arr.push("d")); // agrega un espacio
console.log(arr);
console.log(arr.pop()); // elimina la última observacion
console.log(arr)
var arr2 = ["g","q"];
console.log(arr.concat(arr2)); // concatena o junta los array
console.log(arr.join("!")); // pone un caracter entre cada objeto y luego lo junta en un estring
console.log(arr.reverse()); // ordena invertidamente el array permanentemente
console.log(arr.shift()); // devuelve el primer caracter y lo elimina de la memoria
console.log(arr)
console.log(arr.unshift("p")) // introduce en el primer casille el valor indicado
console.log(arr);
console.log(arr.slice(1,3)) // devuelve objetos entre la posicion 1 y 3
arr.push("i");
arr.push("f");
console.log(arr);
/* 1.10--------------- copying arrays ----------------------- */
var original = [true,true, undefined,false,null];
// slice
var copy1 =original.slice(0);
console.log(copy1)
/* 2 .--------------------------- LOOPS ----------------------------*/
var ourArray = []
for (var i = 0; i < 5; i++){
ourArray.push(i);
}
console.log(ourArray)
var ourArray = []
for (var i = 0; i < 5; i++){
if(i > 2) break;
ourArray.push(i);
}
console.log(ourArray)
var arr = [10,9,8,7,6];
for (var i = 0; i < arr.length; i++){
console.log(arr[i]);
}
var arr = [[1,2],[3,4],[5,6]];
for (var i = 0; i < arr.length; i++){
for (var j=0; j< arr[i].length;j++){
console.log(arr[i][j]);
}
}
var n = 0;
while (n<5){
n++;
console.log("n = " + n);
}
var i = 0;
do{
i++;
console.log("i = " + i)
}while (i < 5);
/* --------------- 2.1 Array iterations ------------------------- */
// forEach
[1,2,3].forEach(function(item,index){
console.log([item,index]);
});
// map
var three = [1,2,3];
var double = three.map(function(item){
return item * 2;
});
console.log(double);
// filter
var inst = [1,2,3];
var evens = inst.filter(function(item){
return item % 2 === 0;
});
console.log(evens);
// reduce
var sum = [1,2,3].reduce(function(result,item){
return result + item;
},0);
console.log(sum)