-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.js
More file actions
431 lines (280 loc) · 9.6 KB
/
Copy pathfirst.js
File metadata and controls
431 lines (280 loc) · 9.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//Arrow Function-
//function declaration
function breakfastMenu() {
return "I'm going to scrambled eggs for breakfast";
}
//anonymous function
const lunchMenu = function() {
return "I'm going to eat pizza for lunch";
}
const dinnermenu= (food)=> `I'm going to eat a ${food} for dinner` //we don't need to specify return
const dinnermenu2= food2=> `I'm going also to eat a ${food2} for dinner` //don't need to add paranthesis if there is only one argument
const dinnermenu3= (...food)=> `I'm going to eat a ${food} for dinner` //using both arrow and spread operator
console.log(dinnermenu("pizza"));
console.log(dinnermenu2("pasta"));
console.log(dinnermenu3("pasta ", "pizza ", 'dosa'));
//template literals-
let x= 'A'
let y= 'B'
const z= `${x} says hello to ${y}`
console.log(z)
// let consta= 'This is the first ES6 file';
// document.getElementById("para").innerText= consta;
//destructuring objects-
const player = {
named: 'Lebron James',
club: 'LA Lakers',
address: {
city: 'Los Angeles'
}
};
console.log(player.named);
const {named, club, address: {city}}= player;
console.log(`${named} plays for ${club}`);
console.log(`${named} lives in ${city}`);
//destructuring arrays-
let [firstName, middleName, lastName] = ['Dylan', 'Coding God', 'Israel'];
console.log(firstName + " " + lastName);
lastName = 'Shah';
console.log(lastName);
//Object literals-
function addressMakerr(city, state) {
const newAdress = {city, state};
console.log(newAdress);
}
addressMakerr('Austin', 'Texas');
//all of the above-
function addressMaker(address) {
const {city, state}= address;
const newAddress = {
city,
state,
country: 'United States'
};
console.log(`${newAddress.city} and ${newAddress.state} of ${newAddress.country}`);
}
addressMaker({city: 'Austin', state: 'Texas'});
//for of loop-
let incomes= [62000,67000, 75000]
let total= 0
for (const income of incomes){
total+= income
}
console.log(total)
// Challenge-
// Using the For of Loop, iterate through the array and print into the console, a message like:
// Tom lives in Lisbon
const students = [
{ name: "John", city: "New York" },
{ name: "Peter", city: "Paris"},
{ name: "Kate", city: "Sydney" }
]
const [first, second, third]= students;
for (const studentss of students){
console.log(studentss.name + ' lives in ' + studentss.city);
}
console.log(`${second.name} lives in ${second.city}`);
// Spread operator-
let contacts = ["Mary", "Joel", "Danny"];
let personalFriends = contacts;
let personalFriends2 = [...contacts];
let personalFriends3 = ["Shrey",...contacts, "Shah"];
contacts.push("John");
console.log(personalFriends); //will change beacause it is a reference
console.log(personalFriends2);
console.log(personalFriends3);
let person={
name: "Adam",
age: 25,
city: "Manchester"
}
let employee={
...person,
salary: 50000,
position: "Software Developer"
}
console.log(employee);
//challenge
//Imagine you are going out to do some grocery shopping. So you have an array called shoppingList with all the products you want to buy. Now that you are inside of the shop, you have a basket with all the products from your list, but you want to add a few more. Create a new array called shoppingBasket, that will be a copy of the shoppingList array, and add some new products into it.
const shoppingList = ["eggs", "milk", "butter"];
const newlist= [...shoppingList, "bread", "crossoint", "peanutbutter"]
console.log(newlist)
//Rest Operator-
function add(nums){
console.log(arguments) //Misc.- Will print out all the arguments in terms of an object
console.log(nums)
}
function add2(...nums){
console.log(nums)
}
add(4,5,6,7,8,9)
add2(4,5,6,7,8,9)
//Default Params-
const leadSinger = (artist = "Shrey") => {
console.log(`${artist} is the lead singer of Cold Play`);
}
leadSinger();
// Default Params Challenge-
//Create a function that receives a parameter of food.
//If your parameter food is going to have a value of "milk" the function should print into the console the following: "I'm going to buy milk from the grocery shop"
//But if you dont pass a value to your parameter food, it should print :"I'm going to buy something from the grocery shop"
const grocery= (food= 'something')=>{
if (`${food}` != null){
console.log(`I'm going to buy ${food} from the grocery shop`)
}
else{
console.log(`I'm going to buy ${food} from the grocery shop`)
}
}
grocery()
// includes()-
let numArray= [1,2,3,4,5]
console.log(numArray.indexOf(0)) //old method for finding inclusivity
console.log(numArray.includes(0)) //here it returns a boolean value
//includes() challenge-
const listIngredients = [ "flour", "sugar", "eggs", "butter" ];
if(listIngredients.includes('chocolate')){
console.log('We are going to make a chocolate cake')
}
else{
console.log("We can't make a chocolate cake because we are missing the ingredient chocolate")
}
//let & const- (does not use hoisting)
const example= 5;
console.log(example)
var example2= [];
example2.append= 'a';
console.log(example2);
//padStart() & padEnd()-
let namedd= 'Shrey';
console.log(namedd.padStart(10,'p'));
console.log(namedd.padEnd(10,'z'));
//Trailing comma-
function add3(param1,){
const example = {
name: 'Dylan',
};
console.log(example)
};
add3(2);
//Promises- used in network requests or APIs
const buyFlightTickets=()=>{
return new Promise( (resolve, reject)=>{
setTimeout(()=>{
const error= false; //flag
if(error){
reject("Sorry your payment wasn't successful")
}
else{
resolve("Thank you, your payment was successful")
}
}, 3000)
})
}
buyFlightTickets()
.then( (sucess)=>{
console.log(sucess)
})
.catch((error)=>{
console.log(error);
});
//if everything goes good, then due ti resolve- the then function will be executed otherwise catch will.
//challenge= promises-
/**
* Promises - Challenge
* Create a promise that returns some user data and throws an error when not found.
* Log the user data when listening to the promise as well as log the error.
*
* Docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
*/
const users= {
Name: 'Harry',
Payment: 500000,
Appointment: true
}
const funcforpromise = ()=>{
return new Promise((resolve, reject)=>{
setTimeout(()=>{
const error= false;
if(error){
reject('Sorry bro couldn\'t authenticate you')
}
else{
resolve(`${users.Name} Bhai`)
}
}, 4000)
})
}
funcforpromise()
.then( (sucess)=>{
console.log(sucess)
})
.catch((error)=>{
console.log(error);
});
//Processing continues to the next link of the chain even when a .then() lacks a callback function. Therefore, a chain can safely omit every rejection callback function until the final .catch().Handling a rejected promise in each .then() has consequences further down the promise chain. Sometimes there is no choice, because an error must be handled immediately. In such cases we must throw an error of some type to maintain error state down the chain. On the other hand, in the absence of an immediate need, it is simpler to leave out error handling until a final .catch() statement. A .catch() is really just a .then() without a slot for a callback function for the case when the promise is fulfilled.
//Fetch
// -> GET
fetch('https://jsonplaceholder.typicode.com/photos/1') //return promise
.then((response)=> response.json()) //also returns a promise
.then((json)=> console.log(`${json.url}`));
// ->POST
fetch('https://jsonplaceholder.typicode.com/comments', {
method: 'POST',
body:JSON.stringify({
postID: 1,
nname: 'ShreyShah',
email: 'shrey@gmail.com',
body: 'That\'s great dude! Keep it up.'
})
})
.then((response)=> response.json())
.then((json)=> console.log(json));
//challenge- fetch-
// * GET the first comments value 'https://jsonplaceholder.typicode.com/comments/1' and log its value.
//* POST a new comment using 'https://jsonplaceholder.typicode.com/comments' and log its value.
fetch('https://jsonplaceholder.typicode.com/comments/1')
.then((response)=> response.json()).then((json)=> console.log(json));
fetch('https://jsonplaceholder.typicode.com/comments', {
method: 'POST',
body: JSON.stringify({
postID: 2,
id:2,
nnname: 'Awesome ShreyShah'
})
})
.then((response)=>response.json())
.then((json)=> console.log(json))
// async and await-
const photos= [];
async function photoupload(){
let uploadstatus= new Promise((resolve,reject)=>{
setTimeout(() => {
photos.push('Profile Picture')
resolve('Picture uploaded')
}, 4000);
})
let result= await uploadstatus;
console.log(result);
console.log(photos.length);
console.log(`${photos}`);
}
photoupload();
//challenge-
async function randomjoke(){
let getjoke= new Promise((resolve,reject)=>{
setTimeout(() => {
fetch('https://api.chucknorris.io/jokes/random')
.then((request)=> request.json())
.then((json)=> resolve(`${json.value}`));
}, 5000);
})
let result2= await getjoke;
console.log(result2);
}
randomjoke();
//Sets in ES6- unique value for arrays
const example3= new Set([1,1,1,1,3,2,4]);
example3.add(6);
console.log(example3.has(2));
console.log(example3);