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
//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
+
150
+
// /Note that in the below codebase we are calling a function inside another function. For eg - function calc is calling 2 functions sum and difference.
149
151
functioncalc(a,b,type){
150
152
if(type=="+"){
151
-
returnsum(a,b);
153
+
returnplus(a,b);
152
154
}
153
155
elseif(type=="-"){
154
156
returndifference(a,b);
155
157
}
156
158
}
157
-
functionsum(a,b){
159
+
functionplus(a,b){
158
160
returna+b;
159
161
}
160
162
functiondifference(a,b){
@@ -199,17 +201,61 @@ console.log(greet); //calling greet function
199
201
//setTImeot is a fuction that takes 2 arguments where one of the argument is a function. This is called callbacks
200
202
201
203
204
+
//Are we allowed to call a function inside another fuction - yes
205
+
206
+
functionsquare(n){
207
+
returnn*n;
208
+
}
209
+
functionsquaresum(a,b){
210
+
letval1=square(a);
211
+
letval2=square(b);
212
+
returnval1+val2;
213
+
}
214
+
console.log(squaresum(4,3));
215
+
216
+
217
+
218
+
//Are we allowed to use a function as a parameter for another function - yes and its called callback function
219
+
functionsq(a){
220
+
returna*a;
221
+
}
222
+
functioncb(a){
223
+
returna*a*a;
224
+
}
225
+
functioncalcsum(num1,num2,callback){
226
+
constval1=callback(num1);
227
+
constval2=callback(num2);
228
+
returnval1+val2;
229
+
}
230
+
console.log(calcsum(10,10,sq));
231
+
console.log(calcsum(10,10,cb));
232
+
233
+
//Are we allowed to use a function as a parameter ? - yes and its called anonymous function
234
+
235
+
//these functoions dont require any function name because they cant be called anywhere lese if they are passes as a parameter , so these nameless functions are called anonymous function
236
+
237
+
238
+
functioncalcmodulo(a,b,fn){//3 parameters, 2 numbers and a function
239
+
console.log(fn)//we are logging the function to see its type.on line 243 we see the argument directly has the fucnction body . This is called anonymous function
240
+
constmod=fn(a,b);//now we pass the fn and save its result in mod variable
241
+
returna+b+mod;//return statement
242
+
}
243
+
console.log(calcmodulo(13,7,function(a,b){//function as argument has to do the a%b
244
+
returna%b;
245
+
}))
246
+
247
+
248
+
249
+
250
+
202
251
203
252
//Assignment 1
204
253
// Question1 - create a counter in js that counts from 30 to 0
205
254
// Question2 - calculate the time it takes between the setTimeout call and the inner fuction actually running
0 commit comments