-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
53 lines (44 loc) · 1.74 KB
/
Copy pathfunction.js
File metadata and controls
53 lines (44 loc) · 1.74 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
const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);
// Example 1: Reverse and capitalize a string
const reverseString = (str) => str.split('').reverse().join('');
const capitalizeString = (str) => str.toUpperCase();
const reverseAndCapitalize = compose(capitalizeString, reverseString);
// Example 2: Double all even numbers in an array
const doubleEvenNumbers = (arr) => arr.map(num => (num % 2 === 0 ? num * 2 : num));
// Transform an array of students
const addStudentStatus = (students) => students.map(student => ({
...student,
status: student.grade > 50 ? 'Pass' : 'Fail'
}));
// Sort array of objects by a given key
const sortByKey = (arr, key) => arr.sort((a, b) => (a[key] > b[key] ? 1 : -1));
// Filter by keyword
const filterByCategory = (products, category) => products.filter(product => product.category === category);
// Simple caching function
const cacheFunction = (fn) => {
const cache = {};
return (...args) => {
const key = JSON.stringify(args);
if (cache[key]) return cache[key];
const result = fn(...args);
cache[key] = result;
return result;
};
};
// Example usage
const students = [
{ name: 'Alice', grade: 55 },
{ name: 'Bob', grade: 45 },
{ name: 'Charlie', grade: 75 }
];
console.log(addStudentStatus(students));
const products = [
{ name: 'Laptop', category: 'Electronics' },
{ name: 'Shoes', category: 'Fashion' },
{ name: 'Phone', category: 'Electronics' }
];
console.log(filterByCategory(products, 'Electronics'));
const expensiveCalculation = (num) => num * num;
const cachedCalculation = cacheFunction(expensiveCalculation);
console.log(cachedCalculation(5)); // Computed
console.log(cachedCalculation(5)); // Cached result