-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSFile.js
More file actions
672 lines (464 loc) · 15 KB
/
Copy pathJSFile.js
File metadata and controls
672 lines (464 loc) · 15 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// alert("Hello World");
// console.log("Hello");
// console.error("Error here");
// console.warn("Warning here");
////// Variables //////
// var -- global scope variable
// let -- local scope, reassign value once declared: YES
// const -- local scope, reassign value once declared: NO
// let age= 31;
// age= 20;
// console.log(age);
// const age2= 75;
// age2= 20; //gives error as const value cant be changed
// console.log(age2);
////// Data Types //////
//String, number, boolean, null, undefined, symbol
// const str='World'; //string
// const num= 40; //number
// const num2= 50.2; //number
// const iscorrect= true; //boolean
// const nullvar= null; // variable but nothing stored in it (null variable)
// const undefvar= undefined; // undefined variable
// let undefvar2; // is also undefined variable
// console.log(typeof str);
// console.log(typeof num);
// console.log(typeof num2);
// console.log(typeof iscorrect);
// console.log(typeof nullvar);
// console.log(typeof undefvar);
// console.log(typeof undefvar2);
//////String Concatenation//////
const str='World';
const num= 40;
//Old method
console.log('The string is '+ str +' and num is '+ num);
//OR
//Template strings for concatenation
console.log(`The string is ${str} and num is ${num}`); // use backticks for template strings
const concatStr=`The string is ${str} and num is ${num}`;
console.log(concatStr);
// .length property -- properties dont have parenthesis i.e. it is just .lengt
const newStr= "A new String";
console.log(newStr.length);
//.toUpperCase() method -- methods have parenthesis i.e. it is .toUpperCase()
const letters= "lowercase letters";
console.log(letters.toUpperCase());
//substring method -- String count always start at zero
const count="It is count string";
console.log(count.substring(0,4));
// here takes substring starting at position 0 and ending at position 3.
//split method -- splits string
const LargeStr= " this string needs to split";
console.log(LargeStr.split(""));
// splits each character in string including spaces
const ListOfThings= " Code, Eat, Sleep, Repeat"
console.log(ListOfThings.split(", "));
//splits whenever a comma and space found
////// Arrays /////
//unlike other languages arrays in JavaScript can store different data types in single array
//JavaScript is not statically typed i.e. const name:string --- like this declaration isnt needed
const numbers= new Array(1,'tree', 4.5, true); // Array() is a constructor
console.log(numbers);
//OR
const fruits=['oranges', 'pears',1,true, 4.5];
console.log(fruits);
console.log(fruits[1]);
////adding elements at end
fruits[5]='tree';
console.log(fruits);
//OR
//push() method
fruits.push(647);
console.log(fruits);
// manipulating const arrays
fruits[5]='tree2'; // we're able to manipulate/change the value in array at position fruits[5] ('tree' to 'tree2').
// This is beacause we cannot reassign complete array but can manipulate it when declared as const fruits[...];
console.log(fruits);
// unshift() method
// adds variable at beginning
fruits.unshift(220);
console.log(fruits);
//// delete last element
// pop() method
fruits.pop()
console.log(fruits);
//// other methods
console.log(Array.isArray(fruits));
console.log(Array.isArray('t'));
console.log(fruits.indexOf(220));
// many other methods possible
////// Object Literals //////
const person={
firstname: 'NameHere',
lastname: 'Surname',
age: 74,
// array within object
hobbies: ['sports', 'movies'],
// object within object
address:{
street: 754,
city: 'Houston',
mobile: 'NA'
}
}
console.log(person);
console.log(person.firstname, person.hobbies);
console.log(person.address.city, person.hobbies[0]);
/// Destructuring
// pulling out variables from object and making them independent variables
// for eg. here firstname & lastname are copied from person (as =person given) and values are pasted to new variables of same name, i.e. it is same as const firstname='NameHere';
const {firstname, lastname}=person;
console.log(firstname);
console.log(lastname);
console.log(typeof firstname);
const {hobbies, address:{city}}=person;
console.log(hobbies);
console.log(city);
// adding elements to object
person.email="abc@123.com";
console.log(person);
//// array of objects
const people=
[
{
id: 1,
text: "take out trash",
isComplete: false
},
{
id:2,
text: "Meeting with Boss",
isComplete: true
},
{
id: 3,
text: "Dentist Appointment",
isComplete: false,
}
];
console.log(people);
console.log(people[1]);
console.log(people[0].text);
//convert object literal to json file text
const peopleJSON = JSON.stringify(people);
console.log(peopleJSON);
////// Loops //////
////// for loop //////
for(let i=0; i<5; i++)
{
console.log(i);
}
for( let i=0; i<people.length;i++)
{
console.log(people[i].text);
}
////// while loop //////
let i=7;
while(i<10)
{
console.log(`While loop count: ${i}`);
i++;
}
////// for..of loop //////
for(let iter of people)
{
console.log(iter.isComplete);
}
////// forEach() //////
people.forEach(
function(iter)
{
console.log(iter.text);
}
);
////// map() //////
const peopleText= people.map(
function(iter)
{
return iter.text;
}
);
//gives an array peopleText extracting text variable from all objects of given array people[].
console.log(peopleText);
////// filter() //////
const peopleisComplete= people.filter(
function(iter)
{
return iter.isComplete==true;
}
);
// gives an array of objects which satisfy the given condition
console.log(peopleisComplete);
//we can also map the given array directly
const peopleisComplete2= people.filter(
function(iter)
{
return iter.isComplete==true;
}
).map(
function(iter)
{
return iter.text;
}
)
console.log(peopleisComplete2);
////// Conditionals //////
////// if else //////
const x= '10';
if(x==10) // == checks value only
{
console.log("x equals 10");
}
if(x===10) // === checks value as well as data type
{
console.log("x has same data type as 10 ");
}
else
{
console.log(" x doesn't have same data type as 10");
}
// we can also add " else if(condition){} " in here
if(x>4)
{
console.log(x);
}
const y=8;
if(x>4 && y<9)
{
console.log("y less than 9 & x more than 4");
}
////// ternary operator //////
const number=10;
const color= number>11 ? "red" : "blue";
console.log(color);
////// switch //////
switch(color)
{
case "red":
console.log("color is red");
break;
case "blue":
console.log("color is blue");
break;
default:
console.log("color undefined");
break;
}
////// functions //////
function add(num1, num2)
{
console.log(num1+num2);
}
add(4,5);
function add2(num1=7, num2=4)
{
return (num1+num2);
}
console.log(add2());
console.log(add2(8,5));
////// arrow functions //////
// function keyword not needed
//1st type
const add3= (num1, num2)=>{
return num1 + num2;
}
console.log(add3(4,10));
// 2nd type
// return keyword not needed
const add4= (num1, num2)=> num1 + num2;
console.log(add4(25,84));
//example
people.forEach((iter) => console.log(iter.text));
//this keyword remaining
////// Object Oriented Programming //////
////// Classes //////
// declaration using function prototyping -- ECMAScript 5
function Person( FirstName, LastName, Country, date)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.Country = Country;
this.date =new Date(date);
this.getBirthYear = ()=> this.date.getFullYear();
}
Person.prototype.getFullName= function() {
return `${this.FirstName} ${this.LastName}`;
};
// Note: Here Date() is predefined Class in JS
//getBirthYear is a local variable defined with arrow function
//getFullYear() is predefined function in JS
const p1= new Person('NameHere','Surname','CountryHere','1-1-2000');
console.log(p1);
const p2= new Person('Name2','Surname2','Country2','1-1-1998');
console.log(p2.FirstName);
console.log(p2.date);
console.log(p2.getBirthYear());
console.log(p2.getFullName());
//declare class using class keyword -- ECMAScript 6
class Person2{
constructor(FirstName, LastName, Country, date)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.Country = Country;
this.date =new Date(date);
this.getBirthYear = ()=> this.date.getFullYear();
}
getFullName() {
return `${this.FirstName} ${this.LastName}`;
};
}
const p3= new Person2('Name3','Surname3','Country3','1-10-1896')
console.log(p3.getBirthYear());
console.log(p3.getFullName());
//
// Symbol ES6 , this keyword, in Depth DOM remaining
//
////// DOM Manipulation //////
// console.log(window);
// window.alert(1);
////// Single Element //////
////// getElementById() //////
// gets first element with given Id
// console.log(document.getElementById('my-form'));
const form = document.getElementById('my-form');
console.log(form);
////// querySelector() //////
//gets first element with given query
const head1= document.querySelector('h1');
console.log(head1);
////// Multiple Elements //////
////// getElementsByClassName() //////
//gets all elements with given class name -- as a Collection
console.log(document.getElementsByClassName('item'));
////// getElementsByTagName() //////
//gets all elements with given tag name ( html tag) -- as a Collection
console.log(document.getElementsByTagName('div'));
////// querySelectorAll() //////
// selects all elements with given query -- as a NodeList
// Using "." prefix for class selector
console.log(document.querySelectorAll('.item'));
// Using "#" prefix for ID selector
console.log(document.querySelectorAll('#name'));
// Using tag name for tag selector
console.log(document.querySelectorAll('header'));
//example
const item= document.querySelectorAll('.item');
item.forEach((iter) => console.log(iter) );
//example 2
const ul = document.querySelector('.itemsall');
// ul.remove(); //removes ul from page (the items list is in ul )
// ul.lastElementChild.remove(); //removes last element in ul tag
// ul.firstElementChild.textContent = 'hello'; //changes text of first element inside ul to 'hello'
// ul.children[1].innerText = 'textchanged';
////// innerHTML //////
ul.lastElementChild.innerHTML = '<h2> HELLO </h2>';
// //// Changing CSS //////
// const btn = document.querySelector('.btn');
// btn.style.background = 'red';
// ////// Events //////
const btn = document.querySelector('.btn');
// btn.addEventListener('click', (e)=>
// {
// //executes the following instructions after clicking
// e.preventDefault(); // prevents from removing the output from the next line --console.log('click');
// console.log('click');
// console.log(e);
// console.log(e.target);
// console.log(e.target.className);
// document.querySelector('#my-form').style.background = '#ccc';
// document.querySelector('body').classList.add('bg-dark'); // classList gives the CSS class name (here 'bg-dark' class present in 1.css at line 67)
// //and classList.add() adds the given CSS class 'bg-dark' to the 'body' class at line 7.
// }
// );
btn.addEventListener('mouseover', (e)=>
{
//executes the following instructions after mouseover
document.querySelector('body').classList.add('bg-dark');
}
);
btn.addEventListener('mouseout', (e)=>
{
//executes the following instructions after mouseover
document.querySelector('body').classList.remove('bg-dark');
}
);
// // Different events in MDN Documentation remaining
// ul.remove(); // see line 501 and 503 -- const ul = document.querySelector('.itemsall');
const myForm = document.querySelector('#my-form');
const nameInput = document.querySelector('#name');
const emailInput = document.querySelector('#email');
const msg = document.querySelector('.msg'); // div with class='msg'
const userList = document.querySelector('#users') // ul with id='users'
myForm.addEventListener('submit', onSubmit);
function onSubmit(e)
{
e.preventDefault();
if(nameInput.value ==='' || emailInput.value ==='')
{
//if input of name or email are empty following code executes
//alert("Enter credentials");
// msg.innerHTML='<p style="color:red">Enter Credentials</p>'
msg.classList.add('error'); // add CSS Class error to msg
msg.innerHTML='<p>Enter Credentials</p>';
setTimeout(()=>msg.remove(), 3000 ); // inbuilt function , takes time on RHS and function to execute after timeout on left, i.e. setTimeout( function, time);
}
else
{
//alert("Success");
msg.innerHTML='<p style="color:green" > Success </p>';
const li= document.createElement('li');
li.appendChild(document.createTextNode(`${nameInput.value}: ${emailInput.value}`));
userList.appendChild(li);
//Clear Fields
nameInput.value='';
emailInput.value='';
}
console.log(nameInput.value);
}
////// Iterators and Generators //////
////// Iterators //////
// Iterators are custom made for iterating over loop
// In general it takes in start of loop, end of loop, and step to be taken
// It contains next() function and has value: & done:true/false pair for each iteration of the loop which the next function returns
// the iterator executes from start (inclusive) till end (exclusive), i.e. end isnt included in loop
//nextIndex, interationCount, result
function makeRangeIterator(start=0, end=Infinity, step=1)
{
let nextIndex = start; //stores Index of each iterator
let iterationCount = 0;
const rangeInterator =
{
next()
{
let result;
if(nextIndex < end)
{
result = { value:nextIndex ,done:false }; // current iteration result
nextIndex+=step;
iterationCount++;
return result; // current iteration result is returned
}
return result = { value: nextIndex , done:true };// if Index is at end, i.e. loop has finished.
}
};
return rangeInterator; // an iterator const variable, rangeInterator is returned
}
const iteratr=makeRangeIterator(1,5,1); // gives out rangeInterator
// thus "iteratr" is now an iterator.
let result =iteratr.next(); // to get first iteration value
while(!result.done) //while loop isnt finished , i.e. while done is false (done:false).
{
console.log(result.value);
result=iteratr.next();
}
console.log(`Iterated over sequence of size: ${result.value}` );
////// For generators refer both MDN and LogRocket //////
//function* keyword
// it is built-in iterator declared using function*
//yield keyword
// after every "yield" keyword, the iteration is stopped and value stated after yield keyword is returned
//.next() -- to iterate
//.return() -- to stop the loop at any iteration
// we can pass values in .next() & .return() as well, for eg. .next('valueHere'); & .return('valueHere');