-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notes.txt
2033 lines (1350 loc) · 48.8 KB
/
Notes.txt
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
************************************************************************************************
-- Javascript is
1. high level language
2. multi paradigm language
3. object oriented language
4. synchronous language
5. single threaded language
-- It is language of web development it makes web pages and web apps works dynamically.
-- Every browser has embedded javascript engine and console
for eg. chrome has v8 engine to run javascript code
-- The programs in this language is called as script.
-- they can be written in html code or run automatically on page load
-- Node.js
-- It is open source, cross platform, backend javascript runtime environment that runs on v8 engine and executes javascript code outside the browser.
// console.log('Welcome to JS Tutorial')
// let a = 98
// console.log('a : ' + a)
// let b = 89 + 789
// console.log('b : ' + b)
************************************************************************************************
// Variables in JS
************************************************************************************************
console.log('Variables in JS')
var a = 89
console.log(a)
var greet = 'hello'
var is_available = true
console.log(greet)
console.log(is_available)
************************************************************************************************
// Arrays in JS
************************************************************************************************
console.log('Arrays in JS')
// Arrays provides an ordered collection of data which can be of different data types
var arr = [12,'Ashish',true,23.4]
console.log(arr)
// accessing the elements of array
console.log(arr[1])
// replacing the existing element of array
arr[1] = 'Python'
console.log(arr)
// length of array
console.log("The length of array is ",arr.length)
// inbuilt array methods
var arr2 = [34,66,77,21]
// pop element --> remove element from end of array and will return that element
arr2.pop()
console.log('poppped array : ',arr2)
// push element --> add element to end of array
arr2.push(233)
console.log('Pushed array : ',arr2)
// shift method --> removes the first element of array and return that value
arr2.shift()
console.log('Shifted array : ',arr2)
// unshift method --> add the element at index 0 of array
arr2.unshift(123)
console.log('Unshifted array : ',arr2)
************************************************************************************************
// Objects
************************************************************************************************
// Everything in JS is object
// In JS, an object is unordered collection of key-value pairs. Each key-value pair is called as property.
// A key of property can be string and value of property can be any value i.e string ,number, array or even function
// JS provides you with many ways to create object. The most commonly used one is to use the object literal notation.
var person = {
firstName : 'Ashish',
lastName : 'Khedekar',
age : 23,
skills : ['Python', 'JS']
}
// accessing the objects
// 1. dot notation
console.log(person.firstName,person.lastName)
// 2. bracket notation
console.log(person['age'])
var favCricketer = {
firstName : 'Rohit',
lastName : 'Sharma',
age : 34,
isCWCWinner : false,
friends : ['Virat Kohli', 'KL Rahul', 'Shubman Gill'],
address : {
state : 'Maharashtra',
city : {
name : 'Mumbai',
pincode : 400068,
}
}
}
console.log(favCricketer.friends[0])
console.log(favCricketer.address.city.name)
// updating the existing properties
favCricketer.isCWCWinner = true
console.log('Updated object : ',favCricketer)
// add new key value pair
favCricketer.iplTropies = 5
console.log('Added new property : ',favCricketer)
// deleting property
delete favCricketer.isCWCWinner
console.log('Deleted some property : ',favCricketer)
************************************************************************************************
// Conditional Statements
************************************************************************************************
// if - else statement
var myScore = 90
if (myScore > 90){
console.log('Yayy got a bicycle!')
}
else{
console.log("Oops didn't make it this time!")
}
// if - else if - else statement
// Greater than equal to 90 -- Grade A
// 70 to 89 -- Grade B
// 50 to 69 -- Grade C
// Lower than 50 -- Grade F
var marks = 35
if (marks >= 90){
console.log('Student got a Grade A')
}
else if (marks >= 70 && marks <= 89){
console.log('Student got a Grade B')
}
else if (marks >= 50 && marks <= 69){
console.log('Student got a Grade C')
}
else {
console.log('Oops! Student got a Grade F')
}
************************************************************************************************
// Control Statements :
************************************************************************************************
// For Loop
// Loops are basically used to control the flow of program and do repetative task
var nums = [2,3,4,5,6,7,8]
var squaredArr = []
for(var i = 0; i < nums.length; i++){
squaredArr.push(nums[i] * nums[i])
}
console.log('squaredArray : ',squaredArr)
// While Loop
var i = 1
var n = 10
console.log('Printing 1 to 10 using while loop : ')
while (i <= n){
console.log(i)
i++
}
// Do while loop
var j = 11
var m = 10
console.log('Printing using do while loop : ')
do {
console.log(i)
i++
} while (i <= n)
// For-in Loops
// In JS, for-in loops allows you to loop through the properties of the object
var colors = {
primary : 'Blue',
secondary : 'Grey',
danger : 'Red'
}
console.log('Printing using for-in loops :')
for (var color in colors) {
console.log(color + ' -> ' + colors[color])
}
// Iterting arrays using for-in looops
var colorsArray = ['Yellow','Blue','Orange','Pink']
console.log('Printing array using for-in loops :')
for (var color in colorsArray) {
console.log(color + ' -> ' + colorsArray[color])
}
// For-of Loops :
// The for-of loops creates a loop iterating over iterable objects
// includes : string, arrys and arrays-like objects like NodeList and also map and set
// The differene between for-in and for-of is for-in holds key and for-of hold value
var scores = [34,56,89,67]
console.log('Printing using for-of loop :')
for (var score of scores) {
console.log(score)
}
// if we want index to be print or access use entries method
console.log('Printing using entries method : ')
for (var [index, score] of scores.entries()) {
console.log(index + ' -> ' + score)
}
// iterating over string using for-of loops :
var str = 'Ashish'
console.log('Printing string using for-of looop : ')
for (var c of str) {
console.log(c)
}
************************************************************************************************
// Functions in JS
************************************************************************************************
// Functions are block of code that performs some task on invokation.
// Basic fundtion
function greet() { // function defination or declaration
console.log('Hello ! Good Morning')
}
greet() // function invokation or function calling
// Parameters and Arguments
// parameter -> defined in function defination
// arguments -> passed when we call function
function add(a,b) {
console.log(a + b)
}
add(2,3)
add(4,6)
// Functions as Expression
// In this we basically create one anonoymous function and assign it to one variable
var add = function(a, b){
return a + b
}
var sum = add
var result = sum(4,7)
console.log('Result of function as expression : ' + result)
// Hoisting :
// 1. every variable which is called above declaration will result as undefined
// bcz whenever code execution begins first memory allocation is done and then code execution starts
// 2. basic function will give result as they are declared during memory allocation phase
// 3. function expression also reacts same as variables
console.log(a)
sub(2,3)
greet()
var a = 'hi'
function greet(){
console.log('Good Morning')
}
var sub = function(a,b){
console.log(a + b)
}
************************************************************************************************
// ES6 in JS :
************************************************************************************************
// Problems with var keyword
// 1. It allows redeclaration of a variable
var a = 21
var a = 32
console.log(a)
// 2. variables declared with var keyword are not blocked scope they are function scope
var num = 67
if (num === 67){
var num1 = 6909
console.log('Inside if loop : ',num1)
}
console.log('Outside if loop : ',num1)
// with function
function test() {
var c = 87
console.log('Inside for loop : ',c)
}
test()
// console.log('Outside for loop : ',c) --> will give errors as var is function scoped
************************************************************************************************
// let keyword :
************************************************************************************************
// 1. Redeclaration of variable is not allowed
// 2. Reassignment is allowed using let keyword
// 3. It is block scoped
// 4. It added itself in window object
let key = 89
console.log('Before reassigmnent : ',key)
// let key = 90
key = 67
console.log('After reassigmnent : ',key)
if (true){
let newKey = 56
console.log('Inside block : ',newKey)
}
// console.log('Outside block : ',newKey) // will get error
************************************************************************************************
// const keyword :
************************************************************************************************
// 1. Redeclaration and reassignment is not allowed
// 2. It is block scoped
// 3. It added itself in window object
const ans = true
// const ans = false // redeclaration is not allowed
// ans = false
// console.log(ans) // reassignment is not allowed
console.log(ans)
if (true) {
const new_ans = 'true'
console.log(new_ans)
}
// console.log(new_ans) // it is block scoped
************************************************************************************************
// Arrow functions
************************************************************************************************
// Usage of arrow function will be seen after oops module
let test1 = () => {
console.log('This is basic arrow function : ',1)
}
// we can make it one line function
let test4 = () => console.log('This is one line function')
let test2 = a => {
console.log('This is one parameter arrow function : ',a + 6)
}
let test3 = (a, b) => {
console.log('This is two parameter arrow function : ',a + b)
}
// fat arrow function with implicit return
let func = () => 122
console.log(func())
// fat arrow function with implicit return
let func = () => 'This is implicit return function'
console.log(func())
test1()
test2(4)
test3(3,6)
test4()
************************************************************************************************
// template literal and mutli line string :
************************************************************************************************
console.log(`Ashish
is
python
programmer`)
let mon = 'November'
console.log(`This is ${mon} month.`)
************************************************************************************************
// Destructuring :
************************************************************************************************
// The destructuring assigment syntax is JS expression that makes it possible to unpack values
// from arrays, or properties from objects into distinct variables
let arr = ['Hi','I','am','Ashish']
let [w, x, y, z] = arr
console.log(x)
// Destructuring the object
// NOTE : We need to define key as variable name in object destructuring
// let colors = {
// primary : 'Blue',
// secondary : 'Grey',
// danger : 'Red'
// }
// let {primary,secondary,danger} = colors
// console.log(primary)
// console.log(secondary)
// console.log(danger)
// --> We can change key variable name with another name
// let colors = {
// primary : 'Blue',
// secondary : 'Grey',
// danger : 'Red'
// }
// let {primary:p,secondary:s,danger:d} = colors
// console.log(p)
// console.log(s)
// console.log(d)
// --> destructing nested objects
let myobj = {
name : 'Ashish',
age : 23,
gender : 'Male',
address : {
country : 'India',
city : 'Mumbai'
}
}
let {name, age , gender, address : {country}} = myobj
console.log(name)
console.log(age)
console.log(gender)
console.log(country)
************************************************************************************************
// Modules & Modularity
************************************************************************************************
calc.js
===========================
function add(a,b) {
console.log(a + b)
}
function sub(a,b) {
console.log(a - b)
}
function mul(a,b) {
console.log(a * b)
}
function div(a,b) {
console.log(a / b)
}
module.exports = {
addition : add,
substraction : sub,
multiplication : mul,
division : div
}
=========================================
script.js
=========================================
const calculator = require('./calc')
calculator.addition(2,5)
calculator.substraction(8,2)
calculator.multiplication(4,4)
calculator.division(4,2)
=========================================
************************************************************************************************
// Spread & Rest
************************************************************************************************
// Spread
// It is used to do deep copy of the array or object.
// let arr = [3,5,6,7,8]
// let arr1 = arr
// console.log(arr1)
// arr[1] = 789 // Here we change value if index 1 in arr it will affect arr1 too as this method do shallow copy of arr
// console.log(arr1)
// let myobj = {
// name : 'Ashish',
// age : 34,
// gender : 'M'
// }
// let myobj1 = myobj
// console.log(myobj1)
// myobj.age = 23 // same thing happen in objects
// console.log(myobj1)
// To overcome above problem we use spread
let arr = [34,5,6,7]
let arr1 = [...arr]
console.log(arr1)
arr[1] = 89798
// arr1[1] = 90
console.log(arr1)
// for objects
let myobj = {
name : 'Ashish',
age : 34,
}
let myobj1 = {...myobj}
console.log(myobj1)
myobj.age = 23
// myobj1.age = 23
console.log(myobj1)
// ===========================================================================================
// Rest
// It is used whenever we need to take multiple values at a time or we need to store remaining values in one variable
function abcd(a,b,c,...d){
console.log(a,b,c,d)
console.log(d[5].name)
}
abcd(1,2,3,4,5,6,6,7,{name : 'Ashish'})
************************************************************************************************
// Functional Programming
************************************************************************************************
// It is programming paradigm designed to handle functions in better way in JS (mainly pure function)
// Imperative and declarative functions :
// Imperative functions : How to go about a problem (approach/structure/logic)
// Problem : we will be given number and need to check if square of the number is even or not
// Imperative way of writing code
const num = 4
const squared = num * num
let isEven;
if (squared % 2 == 0) {
isEven = true
}
else {
isEven = false
}
console.log('Imperative way of writing code : ',isEven)
// Declarative functions : What to achieve from the problem (shorten the code)
// Declarative way of writing code
const checkForSquare = num => (num * num % 2 == 0 ? true : false)
console.log('Declarative way of writing code : ',checkForSquare(5))
************************************************************************************************
// Impure and Pure Function and its side effects :
************************************************************************************************
// 1. Impure Functions :
// -> It takes same input but gives different output
// -> It uses external factors or resources
// let a = 10
// function addImpure(x) {
// console.log(x+a)
// a++
// }
// addImpure(2) // 12
// addImpure(2) // 13
// addImpure(2) // 14
// 2. Pure Functions :
// -> It takes same input and gives same output
// -> It doesn't use any external factors or resources
function addPure(x,a) {
return x + a
}
console.log(addPure(2,4)) // 6
console.log(addPure(2,4)) // 6
console.log(addPure(2,4)) // 6
************************************************************************************************
// Callback Function :
************************************************************************************************
// It is function which takes another function as an argument which can be invoked later on whenever required.
// writing callback function
function printFirstName(firstname, callback) {
console.log(firstname)
callback('Khedekar')
}
function printLastName(lastname) {
console.log(lastname)
}
printFirstName('Ashish',printLastName) // callback function
// Application of callback function
const isEven = num => {
return num % 2 == 0
}
let checkEvenNumber = (evenFunc, number) => {
const result = (evenFunc(number) ? 'Even' : 'Odd')
console.log(`The given number ${number} is ${result}.`)
}
checkEvenNumber(isEven, 16)
************************************************************************************************
// Higher Order Functions :
************************************************************************************************
// It is function which takes a function as argument or return a function as output
// let do one task we need to make square of all array numbers
// let arr = [1,2,3,4,6]
// let squaredArr = []
// for (let i = 0; i < arr.length; i++){
// squaredArr.push(arr[i] * arr[i])
// }
// console.log(squaredArr)
// In above task we are writing code imperatively, so to overcome this issue we use one higher order function
// i.e map
// map function :
// map will loop through every element of an array and will perform a specific task that you have provided
// it takes a function as argument
// map will always return a new array with result.
// let arr = [1,2,4,5,6]
// let squaredArr = arr.map(function(elem){
// return elem * elem
// })
// console.log('Using map function : ',squaredArr)
// const transaction = [1000,3000,4000,2000,-898,3800,-4500]
// const inrToDollar = 80
// let transactionInDollars = transaction.map((tran)=>{
// return (tran / inrToDollar).toFixed(0)
// })
// console.log('Transaction after converting inr to dollar : ',transactionInDollars)
// // ForEach function :
// // it will loop through every element of an array and will perform a specific task that you have provided
// // it takes a function as argument
// // it will work inside forEach scope doesn't return anything
// const transaction1 = [1000,3000,4000,2000,-898,3800,-4500]
// const dollarToInr = 80
// let transactionInInr = transaction.forEach((amount)=>{
// console.log('Transaction after converting dollar to inr : ',amount * dollarToInr)
// })
************************************************************************************************
// find and findIndex methods :
************************************************************************************************
// find will returns the first element of an array that satisfies the condition
// const transaction = [1000,3000,4000,2000,-898,3800,-4500]
// let firstWithdrawl = transaction.find(function(amount){
// return amount < 0
// })
// console.log('First Withdrawl : ',firstWithdrawl)
// // findIndex will returns the first element index from an array that satisfies the condition
// let firstWithdrawlIndex = transaction.findIndex(function(amount){
// return amount < 0
// })
// console.log('Index of First Withdrawl : ',firstWithdrawlIndex)
// ===============================================================================================
// some and every :
// some will return true or false based on condition
// it will return true even if one satisfies the condition
// const transaction = [-1000,-3000,-4000,-2000,898,-3800,-4500]
// let resultSome = transaction.some(function(amount){
// return amount > 0
// })
// console.log('The result of some : ',resultSome)
// // every will return true or false based on condition
// // it will return true if all satisfies the condition
// let resultEvery = transaction.every(function(amount){
// return amount < 0
// })
// console.log('The result of every : ',resultEvery)
************************************************************************************************
// Filter method :
************************************************************************************************
// filter method will return new array it will return all those elements that satisfies the specific condition
// let arr = [1,3,5,6,8,88,35,454,6457]
// let evenArr = arr.filter(function(num){
// return num % 2 == 0
// })
// console.log('Even numbers : ',evenArr)
// // find all transactions which are positive
// const transaction = [1000,3000,4000,-2000,-898,3800,-4500]
// let depositAmount = transaction.filter(function(amount){
// return amount > 0
// })
// console.log('Credited Amount : ',depositAmount)
// ==============================================================================
// Reduce method :
// It will return one result value after performing some operations inside function
// function which is passed as parameter takes two value (accumulator and value)
// problem : you need to sum every even element of array
// imperative way :
// let arr = [1,2,3,5,10]
// let sumOfEven = 0
// for (let i = 0; i < arr.length; i++) {
// sumOfEven += (arr[i] % 2 == 0 ? arr[i] : 0)
// }
// console.log('Imperative way of finding the even number sum of array : ',sumOfEven)
// declarative way using reduce method:
// let arr = [1,2,3,5,10]
// let sumOfEven = arr.reduce((accumulator, value)=>{
// return (value % 2 == 0 ? accumulator + value : accumulator + 0)
// }, 0)
// console.log('Declarative way of finding the even number sum of array : ',sumOfEven)
// ************************************************************************************************
// Chaining :
// ************************************************************************************************
// chaining will help to process multiple higher order functions in same line one after another.
// problem 1 : you need to filter out all males and return ages of all males
// let persons = [
// {name : 'A',age : 23,gender : 'M'},
// {name : 'B',age : 24,gender : 'F'},
// {name : 'C',age : 27,gender : 'F'},
// {name : 'D',age : 29,gender : 'M'},
// {name : 'E',age : 34,gender : 'M'},
// {name : 'F',age : 36,gender : 'F'},
// {name : 'G',age : 21,gender : 'M'},
// {name : 'H',age : 19,gender : 'F'},
// ]
// will solve without using chaining
// let males = persons.filter(function(male){
// return male.gender == 'M'
// })
// let malesAges = males.map((male)=>{
// return male.age
// })
// console.log('Ages of all males in array without using chaining : ',malesAges)
// using chaining
// let maleAges = persons.filter(function(males){
// return males.gender == 'M'
// }).map((male)=>{
// return male.age
// })
// console.log('Ages of all males in array using chaining : ',maleAges)
// Problem 2 :
// from transaction array filter out positive amount and calculate the total amount