You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//using array of objects print only the male candidates above age 18 from a complex objects
87
+
letuserinfo=[
88
+
{
89
+
username:"indresh",
90
+
gender:"male",
91
+
metadata:{
92
+
phone:123456789,
93
+
address:"asdfgg",
94
+
age:20,
95
+
}
96
+
},
97
+
{
98
+
username:"shakthi",
99
+
gender:"female",
100
+
metadata:{
101
+
phone:12356789,
102
+
address:"asdg",
103
+
age:22,
104
+
}
105
+
},
106
+
{
107
+
username:"rohan",
108
+
gender:"male",
109
+
metadata:{
110
+
phone:2356789,
111
+
address:"dfgg",
112
+
age:10,
113
+
}
114
+
}
115
+
]
116
+
for(leti=0;i<userinfo.length;i++){
117
+
userinfo[i]["gender"]=="male"&&userinfo[i]["metadata"]["age"]>18 ? console.log(`male user above 18 is ${userinfo[i]["username"]}`):"";
118
+
}
119
+
120
+
//FUNCTIONS
121
+
// functions let you abstract out logic out of your program
122
+
// They take argument as input and return value as an output
123
+
//we can think of them as independent program that is supposed to do something when given an input
124
+
//functions can take other functions as input this is called callbacks
125
+
126
+
127
+
//write a function to calculate sum of 2 numbers , let a and b be the parameters and call the funvction with the arguments 12 and 34
128
+
functionsum(a,b){
129
+
letans=a+b;
130
+
returnans;
131
+
}
132
+
letsum1=sum(12,34);
133
+
console.log(sum1);
134
+
135
+
//creating a function that takes input of 2 numbers and the oprator to provide the solution for the arithematic expression
136
+
functionarithmatic(a,b,type){
137
+
if(type=="+"){
138
+
returna+b;
139
+
}
140
+
elseif(type=="-"){
141
+
returna-b;
142
+
}
143
+
}
144
+
console.log(arithmatic(12,23,'+'));
145
+
//In the above codebase we directly used the arithematic operations in the conditional stetements itelf.
146
+
147
+
148
+
//In the below codebase we are trying to use 2 differnce seperate functions to calculate sum and difference but we use condotional statement just to call those 2 function based on the type of operation user needs to perform like sum or difference
149
+
functioncalc(a,b,type){
150
+
if(type=="+"){
151
+
returnsum(a,b);
152
+
}
153
+
elseif(type=="-"){
154
+
returndifference(a,b);
155
+
}
156
+
}
157
+
functionsum(a,b){
158
+
returna+b;
159
+
}
160
+
functiondifference(a,b){
161
+
returna-b;
162
+
}
163
+
console.log(calc(10,10,"-"));
164
+
165
+
166
+
//now instead of using all these conditional statements we are going to use these functins as an argument and this is called callbacks
167
+
168
+
//here we are giving functions add and sub as a parameter and based on the input the fn is called
169
+
170
+
//Callbacks functions can be useful only if the 2 functions viz the function viz calling and the function viz called both have same parameters. Callbacks are not used at places where functions have different parameters
171
+
functioncalculator(num1,num2,operationfn){
172
+
letans=operationfn(num1,num2);
173
+
returnans;
174
+
}
175
+
functionadd(num1,num2){
176
+
returnnum1+num2;
177
+
}
178
+
functionsub(num1,num2){
179
+
returnnum1-num2;
180
+
}
181
+
console.log(calculator(10,20,sub));
182
+
183
+
184
+
//another example of callback function can be understood using setTimeout function
185
+
186
+
//function to greet someone
187
+
functiongreet(){
188
+
console.log("hello world");
189
+
}
190
+
setTimeout(greet,3*1000);
191
+
console.log(greet);//calling greet function
192
+
193
+
194
+
//This setTimeout is a function that takes 2 arguments , one is the function it needs to execute and teh other is the time it shd wait ir freez before executing that function. Here 3*1000 - is 3 sec so it means wait for 3 sec before executing the greet function
195
+
196
+
197
+
//basically if u run this program we can see that greet function is executed only after 3 sec of running the program. so here
198
+
199
+
//setTImeot is a fuction that takes 2 arguments where one of the argument is a function. This is called callbacks
200
+
201
+
202
+
203
+
//Assignment 1
204
+
// Question1 - create a counter in js that counts from 30 to 0
205
+
// Question2 - calculate the time it takes between the setTimeout call and the inner fuction actually running
0 commit comments