-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
170 lines (156 loc) · 2.63 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
Functions :- set of statements that perform specific tasks.
syntax:-
function functionname(parameter){
// set of statements
}
//functioncall
functionname()
=>function will always have return
return
=> to reduce the number of time code get executed
=> one function will always have only one return
/*without function
var a =10;
var b =20;
var c = a+b;
console.log(c);
*/
/*with function but with no return
function add(a,b){
console.log(a+b);
}
function sub(a,b){
console.log(a-b)
}
add(10,20)
sub(20,10)
*/
/*
function arithmetic(a,b){
console.log(a+b);
console.log(a-b);
console.log(a*b);
console.log(a%b);
}
arithmetic(60,20)
arithmetic(100,50)
*/
/*with return
function add (a,b){
return a+b
}
add(10,20)
*/
/* with multiple returns
function arithmetic (a,b){
return [a+b,a-b,a*b,a%b]
}
console.log(arithmetic(50,20));
function arithmetic1 (a,b){
return {
sum: a+b,
difference :a-b,
product: a*b,
reminder: a%b
}
}
console.log(arithmetic1(50,20));
*/
Types of function:-
1.Normal Function:-
/*
print even numbers in an array [1,2,3,4,5,6,7,8,9,10]
Normal Function
var result = [];
function even (arr){
for(var i =0; i<=arr.length-1;i=i+1){
if(arr[i]%2===0){
result.push(arr[i])
}
}
return result
}
console.log(even([1,2,3,4,5,6,7,8,9,10]));
*/
2.Annonyomus Function
/*
Annonyomus Function
var result = [];
var a= function (arr){
for(var i =0; i<=arr.length-1;i=i+1){
if(arr[i]%2===0){
result.push(arr[i])
}
}
return result
}
console.log(a([1,2,3,4,5,6,7,8,9,10]));
*/
3.IIFE Function
/*
IIFE Function
var result = [];
(function (arr){
for(var i =0; i<=arr.length-1;i=i+1){
if(arr[i]%2===0){
result.push(arr[i])
}
}
console.log(result);
})([1,2,3,4,5,6,7,8,9,10])
*/
4. Arrow Function
/*
arrow function
var result = [];
var even = (arr)=>{
for(var i =0; i<=arr.length-1;i=i+1){
if(arr[i]%2===0){
result.push(arr[i])
}
}
return result
}
console.log(even([1,2,3,4,5,6,7,8,9,10]));
*/
Task:-
1. Find the median of two sorted arrays.
2. Rotate and array by k times.
Looping
1.While loop:-
while(condition){
statement
}
example:-
/* while loop
let a = 0;
let b = 0;
while (a<3){
a++;
b += a;
}
*/
/* Infinite loop example it is a bad approach
while (true){
console.log("Hello World");
}
*/
2.do while loop :-
syntax:-
do {
statement
}
while (condition);
example:-
/* Do while
let a =0;
do{
a += 1;
console.log(a);
}while(a<5)
*/
3.For loop
4.for of loop
5.for in loop
6.for each loop
If-else