-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (93 loc) · 4.22 KB
/
Copy pathindex.js
File metadata and controls
99 lines (93 loc) · 4.22 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
// PocketPantry — Unit 0 Final Practice Code Examples
// One example per coding-skill module from Unit 0.
// The shared scenario: a kitchen/pantry management app called PocketPantry
// that helps users track inventory, suggest recipes, and reduce food waste.
// =====================================================================
// Module: Values, Data Types, and Operations
// Pseudocode:
// - A pantry item is a record of name (string), quantity (number),
// pricePerUnit (number), isExpired (boolean), and category (string).
// - Compute the total value of one stocked item using arithmetic.
// =====================================================================
const itemName = "Whole Milk"; // string value
const quantity = 2; // number value
const pricePerUnit = 3.49; // number value
const isExpired = false; // boolean value
const category = "Dairy"; // string value
const totalValue = pricePerUnit * quantity; // arithmetic operation
console.log(`Total value of ${itemName}: $${totalValue.toFixed(2)}`);
console.log(`Category: ${category} | Expired? ${isExpired}`);
// =====================================================================
// Module: Stringing Characters Together
// Pseudocode:
// - Build a multi-line shopping list export using template literals,
// escape characters for an apostrophe inside an item name, and
// special characters \n and \t for layout.
// =====================================================================
const ownerFirstName = itemName.split(" ")[0];
const shoppingList = `Shopping List for ${ownerFirstName}'s House:\n- Eggs\t6\n- \"Baker's Chocolate\"\t1\n- Tomatoes\t3`;
console.log(shoppingList);
// =====================================================================
// Module: Control Structures and Logic
// Pseudocode:
// - Decide whether the user can make a recipe.
// - Required: eggs AND milk AND (cheese OR butter).
// - Forbidden: any allergen.
// =====================================================================
const hasEggs = true;
const hasMilk = true;
const hasCheese = false;
const hasButter = true;
const containsPeanuts = false;
if ((hasEggs && hasMilk && (hasCheese || hasButter)) && !containsPeanuts) {
console.log("You can make this recipe!");
} else {
console.log("Missing ingredients or contains an allergen.");
}
// =====================================================================
// Module: Building Arrays
// Pseudocode:
// - Pantry: array of mixed-type item records.
// - Weekly meal plan: 2D array (7 days x 3 meal slots) built with Array(n).
// =====================================================================
const pantry = [
["Milk", 2, 3.49, false, "Dairy"],
["Eggs", 12, 4.99, false, "Dairy"],
["Flour", 1, 2.79, false, "Baking"]
];
const mealPlan = new Array(7).fill(null).map(function () {
return ["Oats", "Sandwich", "Pasta"];
});
console.log("Pantry:", pantry);
console.log("Wednesday lunch:", mealPlan[2][1]);
// =====================================================================
// Module: Using Arrays
// Pseudocode:
// - Manage a shopping list using .length, bracket notation, push, includes,
// indexOf, and splice.
// =====================================================================
const shoppingItems = ["Tomatoes", "Bread"];
shoppingItems.push("Cheese");
console.log(`You have ${shoppingItems.length} items on the list.`);
console.log("First item:", shoppingItems[0]);
if (shoppingItems.includes("Bread")) {
const breadIndex = shoppingItems.indexOf("Bread");
shoppingItems.splice(breadIndex, 1);
console.log("Bread removed. Updated list:", shoppingItems);
}
// =====================================================================
// Module: Working With Loops
// Pseudocode:
// - Walk every pantry item with a for loop bounded by .length.
// - Warn the user for each expired entry.
// =====================================================================
for (let i = 0; i < pantry.length; i++) {
const itemRecord = pantry[i];
const name = itemRecord[0];
const expiredFlag = itemRecord[3];
if (expiredFlag) {
console.log(`WARNING: ${name} is expired.`);
} else {
console.log(`OK: ${name} is fresh.`);
}
}