-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdextor.js
1841 lines (1345 loc) · 60.3 KB
/
dextor.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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TODO: Primitive Data Types:
//* Primitive data types are immutable and directly store values. JavaScript has seven primitive data types:
//? String
//* Represents textual data.
// const fullName = "Muhammad Usman";
// var email = "muhammadusmanawan88@gmail.com";
// var paymentMode = "Credit Card";
// console.log(typeof fullName); // Output: "string"
// console.log(typeof email); // Output: "string"
// console.log(typeof paymentMode); // Output: "string"
//? Number
//* Represents numeric values, including integers and floating-point numbers.
//* Special numeric values: Infinity, -Infinity, and NaN (Not-a-Number).
// let age = 25;
// let price = 19.99;
// var loggedCount = 34 ;
// console.log(typeof age); // Output: "number"
// console.log(typeof price); // Output: "number"
// console.log(typeof loggedCount); // Output: "number"
//? BigInt
//* Represents integers larger than the Number type can safely represent.
//* BigInt literals are created by appending n to the end of an integer.
// let bigNumber = 123456789012345678901234567890n;
// console.log(typeof bigNumber); // Output: "bigint"
//? Boolean
//* Represents a logical entity and can have only two values: true or false.
// let isLoggedIn = true;
// console.log(typeof isLoggedIn); // Output: "boolean"
// var a= 40;
// var b= 20;
// var answer = a > b ;
// console.log(answer);
//? Undefined
//* Represents a variable that has been declared but not assigned a value.
// let x;
// console.log(typeof x); // Output: "undefined"
//? Null
//* Represents the intentional absence of any object value.
//* Note: typeof null returns "object" due to a historical bug in JavaScript.
// let emptyValue = null;
// console.log(typeof emptyValue); // Output: "object"
//? Symbol
//* Represents a unique identifier. Often used as object property keys to avoid name collisions.
// let id = Symbol("id");
// console.log(typeof id); // Output: "symbol"
// TODO: Non-Primitive Data Types:
//* Non-primitive data types store references to objects and are mutable.
//? Object
//* A collection of key-value pairs. Keys can be strings or symbols, and values can be any data type.
// let user = { name: "Alice", age: 30 };
// console.log(typeof user); // Output: "object"
//? Array
//* A special kind of object that stores a list of values.
// let numbers = [1, 2, 3, 4];
// console.log(typeof numbers); // Output: "object"
//? Function
//* A callable object that executes a block of code. Functions are also a type of object.
// function greet() {
// return "Hello";
// }
// console.log(typeof greet); // Output: "function"
//? Date, RegExp, Map, Set, WeakMap, WeakSet
//* These are specialized object types provided by JavaScript for specific use cases:
// Date: Represents a date and time.
// RegExp: Represents a regular expression.
// Map: A collection of key-value pairs where keys can be any data type.
// Set: A collection of unique values.
// WeakMap, WeakSet: Similar to Map and Set, but with some differences in behavior.
// Weaksets are used to store weak references to objects, which do not prevent garbage collection.
// weakmaps are used to store key-value pairs where the keys are weakly referenced.
// TODO: Dynamic Typing:
//* JavaScript is a dynamically-typed language, meaning the type of a variable is determined at runtime and can change dynamically.
// let variable = 42; // Initially a number
// variable = "Hello"; // Now a string
// console.log(typeof variable); // Output: "string"
//!
const theDictator = 'Admiral General Aladin';
// TODO: String Concatination:
//? String concatenation is the process of combining two or more strings into a single string. In JavaScript, the + operator is commonly used for this purpose.
// console.log("my full name is " + fullName );
// console.log("my full name is ", fullName );
//? Using Template Literals (Preferred)
//* Template literals, introduced in ES6, are a cleaner way to concatenate strings using backticks (`) and ${} for variable interpolation:
// console.log(`my full name is ${fullName}`); // Output: "my full name is John Doe"
//? Using concat() Method
// let firstName = "John";
// let lastName = "Doe";
// console.log("my full name is ".concat(firstName, " ", lastName)); // Output: "my full name is John Doe"
// TODO: Interpolation:
//? Interpolation is the process of embedding or inserting variables or expressions directly into a string. It allows for dynamically constructing strings by substituting placeholders with actual values or computed results.
// var fullName = "Muhammad Usman";
// var courseName = "JavaScript";
// var email = "muhammadusmanawan88@gmail.com";
// PaymentMethod = "Credit Card";
// console.log(`
// With full name = ${fullName}
// And Selected course = ${courseName}
// And with email = ${email}
// And with Payment method = ${paymentMode}
// is welcomed to the website.
// `);
// TODO: Array Operations:
// array = ['red' , 88 ];
// array [2] = 'greeen'; // this is how to add a new element to the array
// console.log(array);
// array.push('blue'); // another way to add a new element to the array
// console.log(array);
// array.pop(); // this is how to delete the last element of the array
// console.log(array);
// array.shift(); // this is how to delete the first element of the array
// console.log(array);
// array.unshift('yellow'); // this is how to add a new element to the start of the array
// console.log(array);
// TODO: How to find Discounted Price:
// var sellingPrice = 100;
// var listingPrice = 1000;
// var discountPercent = ((listingPrice-sellingPrice) / listingPrice) * 100 ;
// var discountedPrice = Math.round(discountPercent); //? this is how to round off the number to the nearest integer
// console.log(discountedPrice);
// TODO: If-Else conxept in javascript:
// var authentic = false;
// if(authentic)
// {console.log('the condition is true');}
// else
// {
// console.log('the condition is false');
// }
//* Pro way of Writing If statement in one line
// authentic ? console.log('condition was true') : console.log('conditoin was false') ;
// TODO: Switch statement Example
// var temprature = 20 ;
// var name = "Usman";
// switch(name)
// {
// case 'Usman':
// return console.log('you have full access');
// break;
// case 'Ahmad':
// return console.log('you are first younger brother of Usman');
// case 'Tayyab':
// return console.log('you are second younger brother of Usman');
// break;
// default:
// return console.log('who tf are you?');
// break;
// }
//! Another Example
// var getuserrole = function (name , role) // this is a unique way of declaring a variable as a function
// {
// switch(role)
// {
// case "admin" :
// return `${name} is the admin , and have all the access`;
// break; // this is unnessesary because after return it is not executed
// case "subadmin" :
// return `${name} is the subadmin , and have access almost same as admin `;
// break;
// case "testmaker" :
// return `${name} is the testmaker and have the authority to create or delete tests `;
// break;
// default :
// return `${name} is the user , and have the limited access`;
// break;
// }
// }
// var value = getuserrole("Usman" , "admin"); //? this must be called after the function because it was assigned to a variable and if it is called before the function it will generate error like undefined
// console.log(value);
/* TODO: Multi line comment
? or by selecting multi lines and pressing ( ctrl + / )
TODO: Falsy Values {important for interview part}
* They will always be considered as false which is a boolean expression
undefined
null
""
0
''
NaN */
// console.log('Hello WORLD by pressing alt and show curser');
// console.log('Hello WORLD by pressing alt and show curser');
// console.log('Hello WORLD by pressing alt and show curser');
// console.log('Hello WORLD by pressing alt and show curser');
// console.log('Hello WORLD by pressing alt and show curser');
/*
// *> press ( alt + up or down key ) to move the line up or down
// *> press ( alt + shift + up or down key ) to copy the line
*/
// TODO: Function conxept which gives output:
// function sayname (name)
// {
// var userName = name ;
// console.log("Hello bro ", userName);
// console.log(`wat are you doing today? ${userName}`);
// }
// var name = "Captain Jack Sparrow";
// sayname(name);
// sayname("Captain Teague");
// TODO: Function conxept and the difference between return statement and console loging:
// function greetings(){
// return "Greetings Admiral General";
// }
// var greet = greetings();
// console.log(greet);
// console.log(greetings());
// // TODO: Function conxept which return boolean values:
// function isEven(number)
// {
// return number % 2 === 0 ;
// }
// //? First example:
// console.log(isEven(3));
// //? Second example:
// var result = [2 , 4 , 6 , 8].every(isEven); // here the function is referenced to a array and checks every element of the array
// console.log(result);
// TODO: Arrow function:
// var result = [2, 4, 6, 8].every((e) => {
// return e % 2 === 0;
// })
// console.log(result);
//* Arrow function can also be used without curly brackets, when we don't want to use return keyword
// var result = [2, 4, 6, 8].every((e) => e % 2 === 0 )
// console.log(result);
//! Third example which shows the difference between callback and regular function:
// function greet(name) {
// console.log("Hello, " + name + "!");
// }
// function processUserInput(callback) {
// const name = "Alice";
// callback(name);
// }
// processUserInput(greet); // Output: Hello, Alice!
//* greet is a callback function because it's passed as an argument to processUserInput.
//* processUserInput calls greet later, using the name parameter.
//! Fourth example which shows how regular functions handle this keyword:
//? gives an error in strict mode because it tries to access a non-existent property of the global object (this)
// function Person() {
// this.age = 0;
// setInterval(function() {
// this.age++; // `this` here refers to the global object, not Person
// console.log(this.age); // NaN or unexpected behavior in strict mode
// }, 1000);
// }
// const p = new Person();
//! Fifth example which shows how arrow functions handle this keyword:
// function Person() {
// this.age = 0;
// const intervalId = setInterval(() => {
// if (this.age < 30) { // Condition: Only increment if age is less than 10
// this.age++;
// console.log(this.age);
// } else {
// clearInterval(intervalId); //? clearInterval() is a built-in JavaScript function that stops a timer set by the setInterval() function. And intervalId is the identifier returned by setInterval() when it was initially called. It's used here to specify which interval should be stopped. // Stop the interval once age reaches 10
// console.log("Age limit reached. Age is :", this.age);
// }
// }, 100);
// }
// const p = new Person();
//! Sixth example which shows Example of Callback Function with Regular Function:
// function displayData(data) {
// console.log(data);
// }
// function fetchData(callback) {
// setTimeout(function() {
// callback("Data received!"); // displayData(data) function
// }, 1000); // this is the delay in milliseconds which is 1 second
// }
// fetchData(displayData); // Output after 1 second: Data received!
//! Seventh example which shows Example of Callback Function with Arrow Function:
// function fetchData(callback) {
// setTimeout(() => {
// callback("Data received!");
// }, 1000);
// }
// fetchData(data => console.log(data)); // Output after 1 second: Data received!
// TODO: Function conxept with global context:
// tipper(20); // simply the function is called before the decleration of the function
// function tipper(a){
// var bill =parseInt(a); // here the number in the string entered will be converted into integer
// console.log(a+5); // one simple way but it will not convert strings into number but will do string concatination
// console.log(bill+5); // or more sophisticated way
// }
// tipper("50");
// TODO: Function conxept with global context + scope chain:
// ? the child function can request from parent function
// var name = "Usman";
// function printName(){
// var name = "Ahmad"; // if this is not initialized then this function will console.log => Usman
// console.log("this is line number 178 ", name);
// printNameTwo();
// function printNameTwo(){
// var name = "Tayyab"; // if this is not initialized then this function will console.log => Ahmad
// console.log("this is line number 183 ", name);
// }
// }
// printName();
// TODO: Creating an array:
// var countries = ["Pakistan", "Russia", "China", "Germany", "Japan"];
// var fruits = new Array("Apple", "Peach", "Mango", "Banana", "Pineapple");
// console.log(fruits);
// console.log(fruits[1]);
// console.log(fruits.length);
//! Make a new array from a array by breaking it:
// console.log(Array.from("Aladeen"));
//! Find the index of element in array:
// console.log(fruits.indexOf("Pineapple"));
// console.log(fruits.indexOf("Aeroplane"));
//! Replace element in a array:
// fruits[2] = "Grapes";
// console.log(fruits);
//! Deleting last element in array:
// fruits.pop();
// fruits.pop();
// console.log(fruits);
//! Adding and Deleting first element in array:
// fruits.unshift("Avacado"); // not recommended
// console.log(fruits);
// fruits.shift();
// console.log(fruits);
//! Fill conxept in array:
// var myArray = [1, 3, 5, 6, 7, 2, 9, 8];
// console.log(myArray.fill(88));
// console.log(myArray.fill("L"));
// console.log(myArray.fill(77 , 2 )); // it will print 77 fron location number 2
// console.log(myArray.fill(99 , 1 , 4)); // it will fill 99 from location number 1 till location number 4, excluding number 4
// * the start range is inclusive and end range is exclusive
//! Filter conxept in array:
// var numberArray = [ 11, 22, 33, 44, 55, 66, 77, 88, 99 ];
// var resultArray = numberArray.filter( (numb) => (numb < 55) ) ;
// console.log(resultArray);
//! Slice conxept in array:
// var users = ["Ted" , "Tim" , "Tony" , "Sam" , "Sad" , "Sod"];
// console.log(users.slice(1, 4));
//! Another example of slice:
// const array = ['red', 88, 'green', 'blue'];
// const indexToReplace = 1;
// const newValue = 'grey';
// // Replace the value at index 1
// const newArray = [
// ...array.slice(0, indexToReplace), // Elements before the index
// newValue, // New value to insert
// ...array.slice(indexToReplace + 1) // Elements after the index
// ];
// console.log(array); // Original array remains unchanged
// console.log(newArray); // New array with replaced value
//! Splice conxept in array:
// * here second value is how much to count from the position mentioned
// users.splice(1, 3 , "HI"); // here 1 is the starting point and 3 is the range
// console.log(users);
// users.splice(1, 2 , "HI", "BYE"); // here starting from position 1 and starting from 1 count 2 and replace them with "HI", "BYE"
// console.log(users);
//TODO: Object conxept:
// * values are stored in key value pair format
// var user = {
// firstName : "Muhammad" ,
// lastName : "Usman" ,
// role : "Admin" ,
// loginCount : 55 ,
// facebookSignedIn : true ,
// };
// console.log(user.lastName);
// console.log(user["lastName"]); // here the key should be provided in " " otherwise error will pop up
// user.loginCount = 88 ;
// console.log(user.loginCount);
// console.log(user);
// console.table(user); // or more sophisticated way
//! Interview Question:
// const a={};
// const b={ key: "b" };
// const c={ key: "c" };
// a[b]=123;
// a[c]=456;
// // console.log(a); //? it will print { '[object Object]': 456 } because the key is converted to string
// console.log(a[b]); // 456 after overwriting the previous value
//TODO: Object conxept with funtions:
// var user = {
// userId: 88 ,
// userName: "Muhammad Usman",
// loggedCount: 33,
// courseList: [],
// buyCourse: function (courseName) // a function by the name of buyCourse is initialized
// {
// this.courseList.push(courseName); // the courseName this function will be passed will be pushed into Array courseList
// },
// getCourseCount: function ()
// {
// return `${this.userName} user has been enrolled in ${this.courseList.length} courses`; // here this keyword makes sure that we are calling userName and courseList from this same object
// },
// getInfo: function()
// {
// return `${this.userName} user with user Id ${this.userId} has logged in ${this.loggedCount} and enrolled in ${this.courseList.length} `; //? simple way
// console.table({ //? more sophisticated way
// "User Name": this.userName, // here this keyword should be used but object name can also be referenced
// "User ID": user.userId,
// "Login Count": user.loggedCount,
// "Courses Enrolled": user.courseList.length
// });
// },
// };
// var courseList = true ; // this was made just to make sure that there can be more variable in the code with same name and they will not mess with each other while calling or refering
// console.log(user.getCourseCount() );
// user.buyCourse("Mongo Db course");
// user.buyCourse("Express.js course");
// user.buyCourse("React.js course");
// user.buyCourse("Node.js course");
// console.log(user.getCourseCount() );
// user.getInfo() ; // marvelous
// TODO: Simple for loop:
// * one simple loop example
// let rows = 5; // Number of rows you want to print
// for (let i = 1; i <= rows; i++) // Outer loop controls the number of lines
// {
// let stars = ''; // Initialize an empty string for each line
// //? add stars to the string for each iteration of the outer loop
// for (let j = 1; j <= i; j++) // Inner loop controls the number of stars on each line
// {
// stars += '*'; // Add a star for each iteration of the inner loop
// }
// console.log(stars); // Print the stars for the current line
// }
// * for loop on array example
// var myArray = ["Punjab", 88 , "K.P.K", "Balochistan", "Sindh", 1947 ];
// for(let i = 0 ; i < myArray.length ; i++)
// {
// if(typeof myArray[i] !== "string") continue;
// //? typeof myArray[i] returns a string that describes the type of this element. For example, it could return "string", "number", "object", etc., depending on the element’s type,
// //! If myArray[i] is not a string, then typeof myArray[i] !== "string" evaluates to true,
// //? When continue is executed, the code below it within the loop is skipped for that iteration.
// console.log(myArray[i]);
// }
// TODO: simpler (For Each) loop used for arrays (it is more of a method on array):
// myArray.forEach( (s) => (console.log(s)) ); // this way
// myArray.forEach( (s) => console.log(s) ); //? or this way totaly same
// TODO: (For Of) loop used for array:
//? for arrays specially
// const names = ["Youtube", "Facebook", "Instagram", "Netflix", "Amazon" ];
// for (const ironman of names){
// console.log(ironman);
// }
// TODO: (For In) loop used for objects:
//? for objects specially
// const symbols = {
// yt: "Youtube" ,
// ig: "Instagram" ,
// fb: "Facebook"
// }
// for (const key in symbols) // here key refers to the key value pair of object "symbols"
// {
// console.log(key); // just to print the keys
// console.log(symbols[key]); // just to print the values
// console.log(`key is ${key} and value is ${symbols[key]} `) // or to print both of them at same time
// // console.log(symbols); // Against each entry of object, the key value pair of object is dumped
// }
// TODO: (This) keyword returning global object:
// var user = {
// name : "ABC",
// age: 23 ,
// rollNo: 101,
// getName : function()
// {
// console.log(user.name);
// console.log("Line 379", this); // here this keyword will dump the whole object, we can get one element by saying this.age to get the age
// function sayHello() // this function can be initialized outside the function of the object but when this keyword will be used it will point the global or windowa object
// {
// console.log("Hello");
// console.log("Line 383", this); // this will points to global or windows object, and will not dump the whole object
// }
// sayHello(); // this is a regular function calling
// },
// }
// user.getName(); // this is not considered as a regular function calling because it is called through an object
// console.log("Line 390", this); // here this keyword refers to the global object or windows object, which in case of node is a global empty object
// TODO: (New) keyword example:
// var User = function(firstName , courseCount )
// {
// this.firstName = firstName;
// this.courseCount = courseCount;
// this.getCourseCount = function()
// {
// console.log(`Course count is: ${this.courseCount}`);
// };
// };
// var object1 = new User("Jack" , 2); // we have to add new to make this not a regular function call in order for the "this" keyword to point to the object rather than global scope and also to creat a new instance of the object
// // console.log(object1); // dumps the whole object with the new values assigned
// // object1.getCourseCount();
// var object2 = new User("Will" , 4);
// // console.log(object2);
// // object2.getCourseCount();
//* >>>>>>>> continued ...
// TODO: Prototype conxept:
// ? to make a function of the object (outside of the object), we use prototype keyword
// User.prototype.getFirstName = function ()
// {
// console.log(`Your first name is ${this.firstName}`);
// }
// ? hasOwnProperty() explained
// if(object2.hasOwnProperty("firstName")) // hasOwnProperty is available in prototype of the object and it will return true or false + //? this is more sophisticated way
// {
// object2.getFirstName(); // gets the first name of object2 by first checking if it exists or not "Will"
// };
// object1.getFirstName(); // gets the first name of object1 bluntly "Jack"
// var object3 = new User("Barbosa" , 8)
// console.log(object3); // dumps the whole object3 with the new values assigned
// object3.getFirstName(); // gets the first name of object3
// object3.getCourseCount(); // gets the course count of object3
// TODO: (Object.create) from MDN conxept:
//? creating a proto or a class which defines how every object will be created
// var User = {
// name: "",
// getUsername: function ()
// {
// console.log(`the user name is ${this.name}`);
// },
// };
// var Jacky = Object.create(User);
// // console.log(Jacky); // here the object is created but the values are not assigned and it will give different results on browser which is "__proto__"
// // Jacky.getUsername(); // this will give empty rusult in name place
// Jacky.name = "Jack Sparrow";
// Jacky.getUsername();
//? not so liked way because values need to be initialized in the {}
// var Barbosa = Object.create(User, { name: {value: "Hector Barbossa"},
// role: {value: "Captain"}, // here new property added to the object
// });
// Barbosa.getUsername();
// var roly = Barbosa.role; // this will pass the role of Barbosa to a variable 'roly'
// console.log(roly); // this will give the role of Barbosa
// TODO: Self Anonymous Executing function or IIFE Immediatly Invoked Function Expression:
// (function (){
// console.log(`Q.>> your name is Jack Sparrow?`);
// console.log(`A.>> Its Captain Jack Sparrow.`);
// }
// )();
// TODO: Lexical Scoping + a little touch to Closure:
// function init ()
// {
// var Name = "Jack"; // this name will only be available as far as the context of the function
// function sayName()
// {
// console.log(Name);
// }
// sayName(); // here the function in the function is called and hence it goes away after the execution
// }
// init();
// TODO: Closure + Design Decisions of Redux("A JS library for predictable and maintainable global state management") which is Javascript state management library (also in React and Angular):
//?! one example
// function init ()
// {
// var Name = "Jack"; // this name will only be available as far as the context of the function //? 'Name' is scoped to 'init'
// console.log("i am init");
// function sayName()
// {
// console.log(Name);
// }
// return sayName; // here the function is passed as a reference and is not called right away hence it will not leave the memory //? returns a reference to 'sayName', without invoking it
// }
// console.log("Calling init for the first time");
// var value = init(); // 'init()' is called here, which outputs "i am init"
// value(); // This will output "Jack", here we are using the returned reference of 'sayName' to call it with 'value()'
//?! second example
// function doAdd(x)
// {
// return function(y) // y is the second argument passed to doAdd() function, inner function which is returned from doAdd() function
// {
// return x+y;
// }
// }
// var add5 = doAdd(4); // here add5 is holding the execution of doAdd , which simply return the reference of the inner function, this was technically y value
// console.log(add5(5)); // this add5 is now passed x value
// console.log(doAdd(3)(4)); // function can also be called like this
//?! third example
// let view;
// function likeTheVideo() {
// let called = 0;
// return function() {
// if (called > 0) {
// console.log( "Already Subscribed 👍");
// } else {
// view = "Dictator";
// console.log("Subscribed 👍");
// called++;
// }
// };
// }
// const subscribe = likeTheVideo(); // Store the returned function
// subscribe(); // Subscribed 👍
// subscribe(); // Already Subscribed 👍
// subscribe(); // Already Subscribed 👍
// TODO: Bind and call concept in closure:
//? in order to use a function of some other object with same properties bind or call is used
// var firstPirate = {
// firstName: "Jack",
// lastName: "Sparrow",
// role: "Captain",
// shipName: "Black Pearl",
// getInfo: function()
// {
// console.log(`
// The First Name is ${this.firstName}
// The Last Name is ${this.lastName}
// The Ship Name is ${this.shipName}
// The Role is ${this.role}
// `);
// },
// }
// var secondPirate = {
// firstName: "Hector",
// lastName: "Barbossa",
// role: "Commodore",
// shipName: "Dauntless",
// }
// firstPirate.getInfo();
// firstPirate.getInfo.bind(secondPirate); // we can pass it as a reference it will not run
// firstPirate.getInfo.bind(secondPirate)(); // or we can directly run this
// var infoOfSecondObject = firstPirate.getInfo.bind(secondPirate);
// infoOfSecondObject(); // or we can call like this
// ? call just calls the function right away
// firstPirate.getInfo.call(secondPirate);
// TODO: Strings in JavaScript:
//* Escaping characters
//? one way
// var string1 = "The Jack whispered, \n \"Why Fight \t When You can Negotiate?.\"" // single quotes can be used for strings containing single quotes, double quotes and backslashes, and vice versa. This is called escaping. here \n adds a new line and \t adds a tab distance.
// // console.log(string1);
//? other way
// var string2 = 'Jack\'s Black Pearl from string 2';
// // console.log(string2);
// var string3 = `Captain Jack Sparrow's Black Pearl`; // backticks are used for multiline strings and template literals are used for string interpolation and best practice for string manipulation
// // console.log(string3);
// var name = "Jack";
// var fullName = `My name is Captain ${name}`;
// console.log(fullName);
// console.log(string3.length);
// console.log(string3.charAt(3));
// console.log(string3.endsWith('3'));
// console.log(string3.includes("Pearl"));
// console.log(string3.toLocaleUpperCase());
// var string4 = "The file located at \"C:\\\\Desktop\\My Documents\\Roster\\names.txt\" contains the names on the roster."
// console.log(string4)
// TODO: Strings in JavaScript:
//* ASCI values of the alphabets
//? ASCII value of 'a' is 97, ASCII value of 'b' is 98, ASCII value of 'c' is 99, ASCII value of 'd' is 100, and so on.
// // Pick a string. Your string can have any number of characters.
// var my_string = "x"; // 120
// // Calculate the ASCII value of the first character, i.e. the character at the position 0.
// var ASCII_value = my_string.charCodeAt(0);
// // Let us print
// console.log(ASCII_value);
//? Printing ASCII values of all the characters in your string
// var my_string = "Mana"; // different from MANA
// // Iterate using a Loop
// for (var i = 0; i < my_string.length; i++) {
// console.log(my_string.charCodeAt(i));
// }
// TODO: Replace and ReplaceAll in string:
//* >>>>>>>> the one used in 3 idiot movie
// const str = "Do Yahoo people really love Yahoo?";
// // const newstr = str.replace("Yahoo", "Google"); // this will replace first occurrences of "Yahoo" with "Google"
// const newstr = str.replaceAll("Yahoo", "Google"); // this will replace all occurrences of "Yahoo" with "Google"
// console.log(newstr);
// TODO: Maps in JavaScript:
//? they are similar to objects but with additional features and are not objects
// var myMaps = new Map();
// myMaps.set("key1", "value1");
// myMaps.set("key2", "value2");
// myMaps.set("key3", "value3");
// myMaps.set("key4", "value4");
// console.log(myMaps);
// for (const key of myMaps.keys()) // for of loop is used to iterate over the keys
// {
// console.log(`key is ${key}`);
// }
// for (let value of myMaps.values()) //? for of loop is used to iterate over the values
// {
// console.log(`value is ${value}`);
// }
// for (const [key, value] of myMaps) //? for of loop is best used to iterate over the key-value pairs for the map , and for of loop is gonna always return key first
// {
// console.log(`key is ${key}, and value is ${value}`);
// }
// myMaps.forEach((key) => console.log(`${key}`)); //? this will print all values of the map because forEach loop thinks that we will be doing something with the values not the indexes
// myMaps.forEach((value) => console.log(`${value}`)); //? this will also print all values of the map
// myMaps.forEach((v, k) => console.log(`key is ${k} and, value is ${v}`)); //? for each is always gonna return the value first
// myMaps.delete("key2");
// console.log(myMaps);
// TODO: Destructuring in JavaScript:
//? Destructuring is a JavaScript expression that allows us to unpack values from arrays or objects into distinct variables. the datatype of the elements in the array or object must match the type of the variables being destructured. LHS ==must_be== RHS. Destructuring can also be used to swap the values of two variables.
//! example for arrays
// var user = ["Jack", "Sparrow", "Captain", "Black Pearl"];
// // var firstName = user[0];
// // var lastName = user[1];
// var [firstName, lastName, role, shipName] = user; // this is destructuring, it is assigning the values of user array to firstName, lastName, role and shipName //? the name of the variables is different from the properties in the array
// console.log(`The First Name is ${firstName}`);
// console.log(`The Last Name is ${lastName}`);
//! example for objects
// const myUser = {
// name: "Jack",
// lastName: "Sparrow",
// role: "Captain",
// shipName: "Black Pearl",
// }
// console.log(myUser.role);
// const {name, lastName, role, shipName} = myUser; // this is destructuring, it is assigning the values of myUser object to name, lastName, role and shipName //? the name of the variables should be same as the properties in the object
// console.log(shipName);
// console.log(myUser);
// TODO: Stringify the Object in JavaScript:
// const settings = {
// username: "Jack Sparrow",
// level: 10,
// health: 100,
// };
// const jsonSettings = JSON.stringify(settings); //? this will convert the object into a JSON string
// const SelectiveJsonSettings = JSON.stringify(settings, ["level", "health"]); //? this will only convert the {level and health} of object into a JSON string
// console.log(jsonSettings);
// console.log(SelectiveJsonSettings);
// TODO: Spread and REST operators in JavaScript:
// var returnMaxValue = Math.max(2,3,4,5,6,7,8,9,10);
// console.log(returnMaxValue);
// var myObj = {}; // an empty object
// Object.assign(myObj, {name: "Jack", lastName: "Sparrow", role: "Captain", shipName: "Black Pearl"}); // this is used to merge two or more objects, here it is assigning the values to myObj object
// console.log(myObj);
//! spread operator
// function addTwoNumbers(a, b)
// {
// return a + b;