-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfurtherTopics.js
More file actions
158 lines (140 loc) · 4.1 KB
/
furtherTopics.js
File metadata and controls
158 lines (140 loc) · 4.1 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
// Array Methods
// 1. forEach
// 2. map
// 3. filter
// 4. reduce
// 5. some
// 6. every
// 7. find
// forEach
// The forEach() method calls a function once for each array element.
// Syntax: arr.forEach(callback(currentValue, index, array) { });
// Example:
let numbers = [1, 2, 3, 4, 5];
numbers.forEach(myFunction);
function myFunction(item, index, array) {
console.log(item + " " + index + " " + array);
};
let arr = [1, 2, 3, 4, 5];
let print = function (el) {
console.log(el);
};
arr.forEach(print);
console.log("Through simple function:-");
arr.forEach(function (el) {
console.log(el);
});
let arr1 = [{
name: "Inam",
marks: 86,
},
{
name: "Ukasha",
marks: 85.5,
},
{
name: "Me",
marks: 85,
}];
arr1.forEach((student) => {
console.log(student.marks);
});
// map
// The map() method creates a new array with the results of applying a provided function on every element
// in this array.
// Syntax: arr.map(callback(currentValue, index, array) { });
// Example:
let num = [1, 2, 3, 4, 5];
let square = num.map(function (x) {
return x * x;
});
console.log(square);
let gpa = arr1.map((el) => {
return el.marks / 10;
});
console.log(gpa);
// filter
let num1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
let even = num1.filter(function (x) {
return x % 2 == 0; // true for even , false for odd
});
console.log(even);
let odd = num1.filter(function (x) {
return x % 2 != 0; // true for odd , false for even
});
console.log(odd);
// every
// The every() method tests whether all elements in the array pass the test implemented by the provided function
// Syntax: arr.every(callback(element, index, array) { });
// Example:
let num2 = [1, 2, 3, 4, 5];
let isEven = num2.every(function (x) { // AND gate
return x % 2 == 0;
});
console.log(isEven); // false
// reduce
// The reduce() method applies a function against an accumulator and each element in the array (from left
// to right) to reduce it to a single value.
// Syntax: arr.reduce(callback(accumulator, currentValue, index, array) { }, initialValue);
// Example:
let num3 = [1, 2, 3, 4, 5];
let sum = num3.reduce(function (a, b) {
return a + b;
}, 0);
console.log(sum); // 15
// default parameters
// In JavaScript, you can define a function with default parameters. If a value isn't provided for
// a parameter when the function is called, JavaScript uses the default value.
// Example:
function sum1(a, b = 5) {
return a + b;
}
console.log(sum1(7, 5));
console.log(sum1(7)); // 12
// spread
// The spread operator is a feature in JavaScript that allows an iterable such as an array or string to
// be expanded in places where zero or more arguments are expected.
// Example:
let num4 = [1, 2, 3, 4, 5];
let num5 = [...num4]; // spread operator
console.log(num5); // [1, 2, 3, 4, 5]
// rest
// The rest parameter syntax, ...args, allows a function to accept a variable number of arguments as
// an array.
// Example:
function sum2(...args) {
return args.reduce((a, b) => a + b, 0);
};
console.log(sum2(1, 2, 3, 4, 5));
function min() {
console.log(arguments);
};
min(1, 2, 3, 4, 5);
// arguments is an array like object of the arguments passed to a function. It contains the values of
// all the arguments passed to the function in the order they were passed in. It is not an
// array, but it can be treated like one.
// Example:
// function min() {
// console.log(arguments);
// };
// min(1, 2, 3, 4, 5);
// Output: [1, 2, 3, 4, 5]
// The arguments object is not available in arrow functions. Instead, you can use the rest
// parameter syntax to achieve the same result.
function sum4() {
return arguments.reduce((sum4, el) => sum4 + el);
};
// console.log(sum4(1, 2, 3, 4, 5)); not used by argruments.reduce
// destructuring
// Destructuring is a feature in JavaScript that allows you to unpack values from arrays, objects,
// maps, sets, and other data structures into distinct variables.
// Example:
let person = {
name: 'John',
age: 30,
occupation: 'Developer',
};
let { name, age, occupation } = person;
console.log(name); // John
console.log(age); // 30
console.log(occupation); // Developer