-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathproject-task.js
More file actions
94 lines (70 loc) · 2.9 KB
/
project-task.js
File metadata and controls
94 lines (70 loc) · 2.9 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
/*
===========================================
🛒 Higher-Order Functions: Product Utilities
===========================================
🎯 Objective:
Students will create and work with higher-order functions to transform and manipulate data.
They will:
- Write higher-order functions that accept callbacks to apply transformations dynamically
- Practice returning functions from higher-order functions for reusable, customizable utilities
- Gain experience using `map`, `filter`, and `reduce` to perform practical data transformations
*/
// ============================================
// 📦 Starting Dataset: Product List
// ============================================
const products = [
{ name: "Laptop", price: 1000, inStock: true },
{ name: "Phone", price: 500, inStock: false },
{ name: "Tablet", price: 800, inStock: true },
{ name: "Monitor", price: 300, inStock: true },
{ name: "Keyboard", price: 100, inStock: false },
];
// ============================================
// 🔧 Tasks
// ============================================
/*
🔹 Task 1: Filter Products by Availability
Create a function `filterProducts` that accepts:
- an array of products
- a callback function
The callback should determine which products to include.
Example: filter by availability or price threshold.
Step-by-Step:
1. Define the `filterProducts` function with appropriate parameters.
2. Use the `filter()` method to apply the callback to the array.
3. Return the filtered result.
*/
/*
🔹 Task 2: Transform Product Names
Use `map()` to create a new array of product names in UPPERCASE.
Step-by-Step:
1. Use `map()` on the products array.
2. Extract and transform the `name` property to uppercase.
3. Store the result in a new variable.
*/
/*
🔹 Task 3: Generate Discounted Prices
Write a higher-order function `applyDiscount` that:
- Accepts a discount percentage as a whole number
- Returns a function that takes in a product object and returns a discounted price
Step-by-Step:
1. Define a function `applyDiscount` with a parameter `discountPercent`.
2. Return a new function that takes a product object.
3. Use this returned function inside a `forEach()` call to add a new property, `salePrice`, to each product object.
4. Print the array of products to verify the new property and value have been added to each product object.
*/
/*
🔹 Task 4: Calculate Total Inventory Value
Use `reduce()` to calculate the total value of products that are currently in stock.
Step-by-Step:
1. Use the `reduce()` method on the products array.
2. Add only the prices of products where `inStock` is true.
3. Store the total in a new variable.
*/
// ============================================
// 🧪 Console Test Your Work
// ============================================
// console.log("Filtered products:", ...);
// console.log("Uppercased names:", ...);
// console.log("Discounted products:", ...);
// console.log("Total value in stock:", ...);