-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrocerylist.js
More file actions
117 lines (95 loc) · 3.53 KB
/
grocerylist.js
File metadata and controls
117 lines (95 loc) · 3.53 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// DOM Elements
const form = document.getElementById('grocery-form');
const groceryTableBody = document.querySelector('#grocery-table tbody');
const pantryTableBody = document.querySelector('#pantry-table tbody');
const budgetInput = document.getElementById('budget');
const remainingBudgetDisplay = document.getElementById('remaining-budget');
// Variables
let groceries = [];
let pantry = [];
let totalCost = 0;
let remainingBudget = 0;
// Handle form submission
// Handle form submission
form.addEventListener('submit', function(event) {
event.preventDefault();
// Get values from input fields
const itemName = document.getElementById('item-name').value;
const category = document.getElementById('category').value;
const quantity = parseInt(document.getElementById('quantity').value);
const price = parseFloat(document.getElementById('price').value);
// Check if input is valid
if (!itemName || !category || isNaN(quantity) || isNaN(price) || quantity <= 0 || price <= 0) {
alert("Please enter valid values for all fields.");
return;
}
// Calculate total cost for the item
const totalItemCost = quantity * price;
// Add item to the grocery list
groceries.push({ itemName, category, quantity, price, totalItemCost });
// Update budget
updateBudget(totalItemCost);
// Add item to the table
addGroceryToTable({ itemName, category, quantity, totalItemCost });
//update pantry table
updatePantryTable();
// Clear the form
form.reset();
});
// Add grocery item to the table
function addGroceryToTable(item) {
// sanity check
if (!item || !item.itemName || !item.category || isNaN(item.quantity) || isNaN(item.totalItemCost)) {
console.error('Invalid item object passed to addGroceryToTable');
return;
}
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.itemName}</td>
<td>${item.category}</td>
<td>${item.quantity}</td>
<td>$${item.totalItemCost.toFixed(2)}</td>
<td><button class="check-off">Check Off</button></td>
`;
groceryTableBody.appendChild(row);
console.log("Grocery list array:", groceries);
// Handle check-off button click
row.querySelector('.check-off').addEventListener('click', function() {
checkOffItem(item);
row.remove();
});
}
// Check off item and update pantry inventory
function checkOffItem(item) {
// Update pantry inventory
let existingItem = pantry.find(p => p.itemName === item.itemName);
if (existingItem) {
existingItem.quantity += item.quantity;
} else {
pantry.push({ itemName: item.itemName, category: item.category, quantity: item.quantity });
}
updatePantryTable();
}
// Update pantry table
function updatePantryTable() {
pantryTableBody.innerHTML = ''; // Clear existing table
pantry.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.itemName}</td>
<td>${item.category}</td>
<td>${item.quantity}</td>
`;
pantryTableBody.appendChild(row);
});
}
// Update budget
budgetInput.addEventListener('input', function() {
remainingBudget = parseFloat(budgetInput.value) - totalCost;
remainingBudgetDisplay.textContent = remainingBudget.toFixed(2);
});
function updateBudget(itemCost) {
totalCost += itemCost;
remainingBudget = parseFloat(budgetInput.value) - totalCost;
remainingBudgetDisplay.textContent = remainingBudget.toFixed(2);
}