1+ // Recommended Task: "The E-Commerce Inventory Manager"
2+ // Objective:
3+ // Build a system to manage products in a store using functions to automate tasks and objects to store complex data.
4+
5+
6+ // Create an empty array called inventory.
7+ let inventory = [ ]
8+
9+ // Create a function called createProduct(name, price, quantity).
10+ // This function should return an object with those three properties.
11+ // Call this function three times to create these items and add them to your inventory array:
12+ // "Laptop", 1200, 10
13+ // "Mouse", 25, 50
14+ // "Keyboard", 100, 20
15+ function createProduct ( name , price , quantity ) {
16+ let products = {
17+ "name" :name ,
18+ "price" :price ,
19+ "quantity" :quantity
20+ } ;
21+ return products
22+
23+ }
24+
25+ let p1 = createProduct ( "Laptop" , 1200 , 10 )
26+ let p2 = createProduct ( "Mouse" , 25 , 50 )
27+ let p3 = createProduct ( "Keyboard" , 100 , 20 )
28+
29+ inventory = [ p1 , p2 , p3 ] //all data was overridden by `p3
30+
31+ // Updating Data (Accessing Constraints)
32+
33+ // Oh no! The "Mouse" price was wrong. Access the second item in your inventory array and update its price to 30.
34+ // Access the "Laptop" (first item) and add a new property category with the value "Electronics".
35+ inventory [ 1 ] . price = 30
36+ console . log ( "After increasing mouse price" , inventory )
37+
38+ // Access the "Laptop" (first item) and add a new property category with the value "Electronics".
39+ // Merging Product Details (Spread Operator):
40+ inventory [ 0 ] . category = "Electronics"
41+ console . log ( "After adding the category on p1" , inventory )
42+ // Create an object called extraDetails with { warranty: "2 years", color: "Silver" }.
43+ // Create a new variable updatedLaptop by merging the first item in your inventory with extraDetails using the spread operator (...).
44+ // Console log updatedLaptop to see the combined result.
45+
46+ const extraDetails = {
47+ warranty :"2 years" ,
48+ color :"Silver"
49+ }
50+
51+ let updatedLaptop = { ...inventory [ 0 ] , ...extraDetails }
52+ console . log ( updatedLaptop )
53+
54+ // The Sale Function (Arrow Functions):
55+
56+ // Write an Arrow Function called calculateTotalValue.
57+ // It should accept two parameters: price and quantity.
58+ // It should return the total value (price * quantity).
59+ // Use this function to calculate the total value of the "Keyboard" stock (from your inventory) and log it.
60+
61+ const calculateTotalValue = ( price , quantity ) => {
62+ return ( price * quantity )
63+ }
64+ const totalValOfKeyboard = calculateTotalValue ( inventory [ 2 ] . price , inventory [ 2 ] . quantity )
65+ console . log ( "The total value of Keyboard is" , totalValOfKeyboard )
66+
67+ // Nested Data Challenge:
68+
69+ // Create a user object called adminUser with:
70+ // name: "Manager"
71+ // permissions: A nested object { canEdit: true, canDelete: false }
72+ // Write an if statement: If adminUser.permissions.canEdit is true, console log "Access Granted: Inventory updated."
73+ // Why this works:
74+ // Functions: They practice passing arguments (name, price) effectively.
75+ // Objects: They learn to store grouped data instead of just single variables.
76+ // Spread: Real-world use case (adding extra specs to a product).
77+ // Nested Access: Reviewing how to dig into data (user.permissions.canEdit).
78+
79+ const adminUser = {
80+ name :"Manger" ,
81+ permissions :{
82+ canEdit :true ,
83+ canDelete :false
84+ }
85+ }
86+
87+ if ( adminUser . permissions . canEdit === true ) {
88+ console . log ( "Access Granted: Inventory updated!!!!" )
89+
90+ }
0 commit comments