-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notes.txt
142 lines (125 loc) · 2.3 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
Functions :- set of statements that perform specific tasks.
syntax:-
function functionname(parameter){
//set of statements
}
//function call
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)
*/
/* 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 Functions:-
1.Normal Functions
// Print even numbersin an array[1,2,3,4,5,6,7,8,9,10]
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
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
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);
4.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]))
Lopping
1.while lopping
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