-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
262 lines (191 loc) · 6 KB
/
function.js
File metadata and controls
262 lines (191 loc) · 6 KB
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
// function = A section of reusable code.
// Declare code once, use it whenever you want.
// Call the function to execute the code.
// Printing function
function happyBirthday(userName, age){
console.log("Happy Birthday to you ");
console.log("Happy Birthday to you ");
console.log(`Happy Birthday dear ${userName}`);
console.log("Happy Birthday to you");
console.log(`You are ${age}`);
}
happyBirthday("BroCode", 46);
// Return function
function add(x,y){
return x + y
}
function subtract(x,y){
return x - y
}
console.log(add(2,3))
console.log(subtract(2,3))
//callback = a function that is passed as an argument to
// another function
// used to handle asynchronous operations
// 1. Reading a file
// 2. Network Requests
// 3. Interacting with databases
// "Hey when you're done , call this next"
// Asynchronous operations in JS are tasks that run in the background without
// blocking the main execution thread , allowing the program to remain responsive and handle
// other events concurrently.
hello(goodbye);
function hello(callback) {
console.log("hello");
callback();
}
function goodbye() {
console.log("goodbye");
}
sum(displayConsole, 1, 2)
function sum(callback, x, y){
let result = x+y;
callback(result);
}
function displayConsole(result){
console.log(result);
}
// setTimeout function: first functionRef then lines of code and timeout
// time in Milliseconds
//forEach() = method used to iterate over the elements of an array
// and apply a specified function (callback) to each element
// array.forEach(callback), element, index, array are provided
let numbers = [1,2,3,4,5];
numbers.forEach(display)
numbers.forEach(double);
function display(element){
console.log(element);
}
function double(element , index, array){
array[index] = element * 2;
console.log(array);
}
//.map() = method used to apply a callback to an array create a
// new array storing the callback applied values.
// It is similar to .forEach() function
let mappingNumbers = [1,2,3,4,5,6]
let mappedNumbers = mappingNumbers.map(cubed)
console.log(mappedNumbers)
function cubed(element){
return Math.pow(element, 3)
}
//.filter() = method to create a new array by filtering out elements.
// It keeps the element that return a 'true' boolean value
// filters out the elements that return a 'false' boolean value
let nums = [1,2,3,3,4,5,2,3,4,6,4,];
let evenNums = nums.filter(isEven);
console.log(evenNums);
function isEven(element){
return Number(element) % 2 === 0;
}
//.reduce() = reduces an array to one single value. It is the sum of the array.The accumulator
// parameter is the previous value and the element is the next value in relation to
// the accumulator.
let prices = [30, 40, 50, 80, 90, 20]
let bill = prices.reduce(sum_fx);
console.log(`$${bill}`);
let max = prices.reduce(getMax);
console.log(max);
let min = prices.reduce(getMin);
console.log(min);
function sum_fx(accumulator, element){
return accumulator + element;
}
function getMax(accumulator, element){
return Math.max(accumulator, element);
}
function getMin(accumulator, element){
return Math.min(accumulator, element);
}
// function expression = a way to define functions as values or variables
//Example of defining functions as variables
const printHello = function(){
console.log("Hello!");
}
printHello();
//Example of defining functions as values:
// Callbacks in asynchronous operations
// High Order Functions
// Closures
// Event Listeners
setTimeout(function(){
console.log("Hello! This came 1 seconds later");
}, 1000); // 1 second later
//arrow functions = are used for writing function expressions in a concise way e.g. (parameters) => some code
const greet = (name) => {console.log(`Hello ${name}`)
console.log(`Your name is ${name}`)
};
greet("Sarthak Roy");
//destructuring = arrange values in a different order for convenience
// [] for arrays
// {} for objects
// Example 1 : Swapping values of variables
let numberA = 1;
let numberB = 2;
[numberA, numberB] = [numberB, numberA];
console.log(numberA);
console.log(numberB);
//Example 2 : Swapping elements of an array
const colors = ["red", "green", "blue", "black", "white"];
[colors[0], colors[4]] = [colors[4], colors[0]];
console.log(colors);
//Example 3: Assign array elements to variables
const colors1 = ["red", "green", "blue", "black", "white"];
let firstColor ;
let secondColor ;
let thirdColor ;
let extraColors;
[firstColor,secondColor, thirdColor, ...extraColors] = colors1;
console.log(firstColor);
console.log(secondColor);
console.log(thirdColor);
console.log(extraColors);
//Example 4 : Extract values from objects
const person1 = {
name : "Sarthak Roy",
age : 12,
job: "Engineer"
}
const {name, age, job="Unemployed"} = person1;
console.log(name);
console.log(age);
console.log(job);
//Example 5: Destructure in function parameters
function displayPerson({name, age, job}) {
console.log(name);
console.log(age);
console.log(job);
}
const person2 = {
name : "Ankita Roy",
age : 18,
job: "Doctor"
}
displayPerson(person2);
//nested objects = Objects inside of objects which helps in making very complex data structures
// e.g. computerSetup{CPU{}, GPU{}, Motherboard{}}
const ComputerSetup = {
place: "Sarthak's Room",
core: "MI Notebook 14",
CPU : {
name: "Intel i3 10100U",
clockSpeed: 2.10
},
GPU : {
name: "Intel(R) UHD Graphics",
GPUMemory: 3.9
},
RAMMemory : {
memory: 8,
type : "DDR4"
}
}
console.log(ComputerSetup.place);
console.log(ComputerSetup.core);
console.log(ComputerSetup.CPU.name);
for (const property in ComputerSetup.CPU){
console.log(ComputerSetup.CPU[property]);
}
for (const property in ComputerSetup.GPU){
console.log(ComputerSetup.GPU[property]);
}