-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
126 lines (119 loc) · 1.89 KB
/
script.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
/*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));
*/
/*
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]));
*/
/*
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]));
*/
/*
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])
*/
/*
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]));
*/
/* Do while
let a =0;
do{
a += 1;
console.log(a);
}while(a<5)
*/
/* 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");
}
*/