0
0
Goprogramming~30 mins

Logical operators in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Logical operators
๐Ÿ“– Scenario: You are helping a store manager decide which products to restock based on their sales and stock levels.
๐ŸŽฏ Goal: Build a Go program that uses logical operators to find products that need restocking.
๐Ÿ“‹ What You'll Learn
Create a map called products with product names as keys and their stock counts as values.
Create a map called sales with product names as keys and their sales counts as values.
Create a variable called restockThreshold to set the minimum stock level before restocking.
Use a for loop with variables product and stock to iterate over products.
Inside the loop, use an if statement with logical operators && and || to check if a product's stock is below restockThreshold or if sales are high.
Print the names of products that need restocking.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Stores often need to decide which products to order more of based on current stock and sales trends.
๐Ÿ’ผ Career
Understanding logical operators and data structures like maps is essential for roles in software development, data analysis, and inventory management systems.
Progress0 / 4 steps
1
Create the products map
Create a map called products with these exact entries: "Apples": 50, "Bananas": 20, "Cherries": 0, "Dates": 15.
Go
Need a hint?

Use map[string]int to create a map with string keys and int values.

2
Create the sales map and restock threshold
Create a map called sales with these exact entries: "Apples": 30, "Bananas": 50, "Cherries": 5, "Dates": 10. Also create an integer variable called restockThreshold and set it to 20.
Go
Need a hint?

Use the same map syntax as before for sales. Use := to create restockThreshold.

3
Use logical operators to find products to restock
Use a for loop with variables product and stock to iterate over products. Inside the loop, use an if statement with logical operators && and || to check if stock is less than restockThreshold or if sales[product] is greater than 30. If so, add the product to a slice called toRestock.
Go
Need a hint?

Use range to loop over the map. Use append to add to the slice.

4
Print the products to restock
Write a for loop with variable product to iterate over toRestock and print each product.
Go
Need a hint?

Use for _, product := range toRestock to loop over the slice and fmt.Println(product) to print.