-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (132 loc) · 3.86 KB
/
Copy pathscript.js
File metadata and controls
192 lines (132 loc) · 3.86 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
// 1. Find Second Largest Number
// const numbers = [10, 25, 8, 40, 30];
// function findSecondLargest (arr) {
// let largest = -Infinity;
// let secondLargest = -Infinity;
// for (let i = 0; i < arr.length; i++) {
//
// if (arr[i] > largest) {
// secondLargest = largest;
// largest = arr[i];
// } else if (arr[i] > secondLargest && arr[i] !== largest) {
// secondLargest = arr[i];
// }
// }
// return secondLargest;
//
// }
//
// console.log(findSecondLargest(numbers));
// 2. Remove Duplicate Values
// const num = [10, 20, 10, 30, 20, 40];
// function removeDuplicates (arr) {
// return arr.filter((value, index) => {
// return arr.indexOf(value) === index;
// });
// }
// console.log(removeDuplicates(num));
// 3. Count Frequency of Numbers
// const numbers = [1, 2, 2, 3, 1, 2];
// function countFrequency(arr) {
// let frequency = {};
// for (let num of arr) {
// if (frequency[num]) {
// frequency[num]++;
// } else {
// frequency[num] = 1;
// }
// }
// return frequency;
// }
// console.log(countFrequency(numbers));
// 4. Employees With Salary Above Average
// const employees = [
// { name: "Arun", salary: 25000 },
// { name: "Kumar", salary: 45000 },
// { name: "Ravi", salary: 30000 },
// { name: "Vijay", salary: 50000 }
// ];
// function aboveAverage(arr) {
// let total = arr.reduce((sum, employee) => {
// return sum + employee.salary;
// }, 0);
// let average = total / arr.length;
// return arr.filter(employee => employee.salary > average);
// }
// console.log(aboveAverage(employees));
// 5. Find Common Elements
// const arr1 = [1, 2, 3, 4, 5];
// const arr2 = [3, 4, 5, 6, 7];
// function findCommon(a, b) {
// return a.filter(num => b.includes(num));
// }
// console.log(findCommon(arr1, arr2));
// 6. Convert Names to Uppercase
// const students = [
// { name: "abi", mark: 75 },
// { name: "karthi", mark: 45 },
// { name: "sundar", mark: 85 }
// ];
// function getNames(arr) {
// return arr.map(student => student.name.toUpperCase());
// }
// console.log(getNames(students));
// 7. Find First Duplicate
// const numbers = [5, 3, 4, 3, 2, 5];
// function firstDuplicate(arr) {
// let seen = [];
// for (let num of arr) {
// if (seen.includes(num)) {
// return num;
// }
// seen.push(num);
// }
// return null;
// }
// console.log(firstDuplicate(numbers));
// 8. Group Students by Result
// const students = [
// { name: "Arun", mark: 75 },
// { name: "Kumar", mark: 35 },
// { name: "Ravi", mark: 60 },
// { name: "Vijay", mark: 40 }
// ];
// function groupStudents(arr) {
// const passed = arr
// .filter(student => student.mark >= 50)
// .map(student => student.name);
// const failed = arr
// .filter(student => student.mark < 50)
// .map(student => student.name);
// return {passed, failed};
// }
// console.log(groupStudents(students));
// 9. Find Most Expensive Product
// const products = [
// { name: "Phone", price: 20000 },
// { name: "Laptop", price: 55000 },
// { name: "Tablet", price: 30000 },
// { name: "Monitor", price: 18000 }
// ];
// function mostExpensive(arr) {
// let expensive = arr[0];
// for (let product of arr) {
// if (product.price > expensive.price) {
// expensive = product;
// }
// }
// return expensive;
// }
// console.log(mostExpensive(products));
// 10. Sum of Only Odd Numbers
// const numbers = [10, 15, 20, 7, 8, 13];
// function sumOdd(arr) {
// let sum = 0;
// for (let num of arr) {
// if (num % 2 !== 0) {
// sum += num;
// }
// }
// return sum;
// }
// console.log(sumOdd(numbers));