-
Notifications
You must be signed in to change notification settings - Fork 339
/
app.js
337 lines (289 loc) · 6.37 KB
/
app.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* * Name: Human_Lifecycle
* * Param: human_name
* * :human_name, awake from sleep
* * :human_name, go to washroom
* * :human_name, take breakfast
* * :human_name, go to school/college/office
* * :human_name, Return from office
* * :human_name, Take dinner
* * :human_name", Go to sleep
*/
// Call Human_Lifecycle for 'Abu Musa'
// Call Human_Lifecycle for 'Easin Islam'
// Call Human_Lifecycle for 'Saiful Islam'
// Call Human_Lifecycle for 'Akib Ahmed'
// Call Human_Lifecycle for 'Alamin Mir'
/**
* Function: Sleep
* Param: name
* Definition: How to sleep
*/
function sleep(name) {
console.log(`${name} is sleeping`);
}
/**
* Function: Awake
* Param: name
* Definition: How to awake
*/
function awake(name) {
console.log(`${name} is awake`);
}
/**
* Function: Eat
* Param: name
* Param: Time
* Definition: How to eat
*/
function eat(name, time) {
console.log(`${name} is taking ${time}`);
}
/**
* Function: Walk
* Param: name
* Param: Destination
* Definition: How to walk
*/
function walk(name, destination) {
console.log(`${name} is walking to ${destination}`);
}
/**
* Function: Study
* Param: name
* Definition: How to study
*/
function study(name) {
console.log(`${name} is studying`);
}
/**
* Function: Work
* Param: name
* Definition: How to work
*/
function work(name) {
console.log(`${name} is working`);
}
/**
* Function: Job_Holder_Lifecycle
* Param: name
* Definition:
* - Awake -> name
* - Eat -> name, 'breakfast'
* - Walk -> name, 'office'
* - Work -> name
* - Eat -> name, 'lunch'
* - Walk -> name, 'home'
* - Eat -> name, 'dinner'
* - Sleep -> name
*/
function jobHolderLifecycle(name) {
awake(name);
eat(name, 'breakfast');
walk(name, 'office');
work(name);
eat(name, 'lunch');
walk(name, 'home');
eat(name, 'dinner');
sleep(name);
}
console.log('Jobholders Lifecycle');
console.log('**********************');
jobHolderLifecycle('Shayed Hasan');
console.log('-----------------------');
jobHolderLifecycle('Sh Pabel');
console.log('-----------------------');
jobHolderLifecycle('Tarikul Islam');
console.log('-----------------------');
jobHolderLifecycle('Nahian Sikder');
console.log('-----------------------');
jobHolderLifecycle('Mizan Rahman');
console.log('-----------------------');
/**
* Function: Student Lifecycle
* Param: name
* Definition:
* - Awake -> name
* - Eat -> name, 'breakfast'
* - Study -> name
* - Eat -> name, 'lunch'
* - Study -> name
* - Eat -> name, 'dinner'
* - Sleep -> name
*/
function studentsLifecycle(name) {
awake(name);
eat(name, 'breakfast');
study(name);
eat(name, 'lunch');
study(name);
eat(name, 'dinner');
sleep(name);
}
console.log('Students Lifecycle');
console.log('**********************');
studentsLifecycle('Faruk');
console.log('--------------------');
studentsLifecycle('Elias');
console.log('--------------------');
studentsLifecycle('Faisal');
console.log('--------------------');
// Students_Lifecycle -> 'Faruk'
// Students_Lifecycle -> 'Elias'
// Students_Lifecycle -> 'Faisal'
// Job_Holder_Lifecycle -> 'Musa'
// Job_Holder_Lifecycle -> 'Akib'
// Job_Holder_Lifecycle -> 'Bipon'
// * Function Template
function name_of_the_function(/** Input something */) {
// Function body
// any valid js code
// return a result
}
// There are two steps
// - Define a function
// - Invoke a function
/* function testFunction() {
const a = 10;
const b = 20;
const result = a + b + Math.max(a, b);
console.log(result);
} */
function testFunction(a = 10, b = 20) {
const result = a + b + Math.max(a, b);
console.log(result);
}
// testFunction(100, 200);
// testFunction(50, 30);
// testFunction(5);
// testFunction();
// function composition
function sum(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function times(a, b) {
return a * b;
}
const a = 10;
const b = 20;
// const r1 = sum(a, b);
// console.log('R1', r1);
// const r2 = subtract(a, b);
// console.log('R2', r2);
const r = Math.abs(times(sum(a, b), subtract(a, b)));
console.log(r);
// Function definition
// Function Invoking
// Function Parameters/Arguments
// Return result from function
// Function is a first class citizen
// We can treat function as value
// 10, 'test', true, false -> function
/**
* * Benefits:
* * - we can store functions in a variable
* * - we can store function inside an object / array
* * - we can pass function as an argument
* * - we can also return a function from another function
*/
// * Proof -> Function is a value
function testFunction() {
console.log('I am a test function');
}
// * store function in a variable
const fn = testFunction;
console.log(fn.toString());
fn();
// * store function inside an object / array
const arr = [fn, testFunction];
const obj = {
fn: testFunction,
};
// * pass function as an argument
function fnArgument(fn) {
return fn();
}
fnArgument(testFunction);
// * return a function from another function
function returnFn() {
return testFunction;
}
// * Let's construct a function
const newFn = new Function(
'str',
`let obj = {};
for (let s of str) {
if (s !== ' ') {
obj[s] = s;
}
}
return obj;`
);
console.log(newFn('HM Nayem'));
const fData = {
params: ['a', 'b'],
body: ['const r = a + b', 'return r'],
};
const fBody = fData.body.reduce((acc, cur) => {
acc += cur + ';';
return acc;
}, '');
const tf = new Function(...fData.params, fBody);
console.log(tf(100, 200));
const greetFn = new Function(
'name',
'email',
`
const fName = name.split(' ')[0];
console.log('Hello,', fName, 'Is that your email?', email);
console.log('Yeah, this is mine.');
return fName;
`
);
console.log(typeof greetFn);
console.log(greetFn.__proto__);
// console.log(greetFn.toString());
const fName = greetFn('HM Nayem', '[email protected]');
console.log('First Name:', fName);
const operations = [
{
args: [10, 20],
params: ['a', 'b'],
body: 'console.log("a + b",a + b)',
},
{
args: [10, 20],
params: ['a', 'b'],
body: 'console.log("a - b",a - b)',
},
{
args: [10, 20],
params: ['a', 'b'],
body: 'console.log("a * b",a * b)',
},
{
args: [],
params: [],
body: 'console.log("Hello World! No params, no args")',
},
{
args: [5],
params: ['n'],
body: `
for (let i = 0; i < n; i++) {
let line = '';
for (let j = 0; j < n; j++) {
line += '* ';
}
console.log(line);
}
`,
},
];
operations.forEach((operation) => {
const fn = new Function(...operation.params, operation.body);
fn(...operation.args);
});