-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (60 loc) · 2.31 KB
/
script.js
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
// Selecting Elements
const itemInputEl = document.getElementById("item");
const quantityInputEl = document.getElementById("quantity");
const shoppingListDisplayEl = document.getElementById("shopping-list-display");
const shoppingBasketInputEl = document.getElementById("shopping-basket");
const itemsStillToBuyDisplayEl = document.getElementById(
"items-still-to-buy-display"
);
//shoppingList is an array [] of objects { item, quantity }
const shoppingList = [];
function addItem(event) {
//Prevent the default behaviour of our button (which is to submit the form and refresh the page)
event.preventDefault();
//Create an Object and then add the object into shoppingList array
const itemToAdd = {
item: itemInputEl.value,
quantity: quantityInputEl.value,
};
shoppingList.push(itemToAdd);
console.log(shoppingList);
itemInputEl.value = "";
displayListItems();
}
function displayListItems() {
//clear the display
shoppingListDisplayEl.innerHTML = "";
//for each item in the shoppingList array, display item, quantity, and a delete button
for (const groceryItem of shoppingList) {
//update the HTML to what is currently rendered, plus a new string
shoppingListDisplayEl.innerHTML += `${groceryItem.item} x ${
groceryItem.quantity
} <span onclick="deleteItem(${shoppingList.indexOf(
groceryItem
)})"> ❎ </span><br/>`;
}
}
function deleteItem(itemToDelete) {
//delete the item use splice method
shoppingList.splice(itemToDelete, 1);
displayListItems();
}
function addToBasket(event) {
event.preventDefault();
//split() splits the inputted form data by commas, and returns a new array
const basket = shoppingBasketInputEl.value.toLowerCase().split(/,\s*/);
const itemsStillToBuy = shoppingList.filter(function (groceryItem) {
return !basket.includes(groceryItem.item.toLowerCase());
});
console.log(itemsStillToBuy);
if (itemsStillToBuy.length === 0) {
itemsStillToBuyDisplayEl.innerHTML =
"<p>Congrats! You are done shopping 🛍️ ➡️ 🏠: </p>";
} else {
itemsStillToBuyDisplayEl.innerHTML =
"Here are the items you still need to get:<br/>";
itemsStillToBuy.forEach(function (groceryItem) {
itemsStillToBuyDisplayEl.innerHTML += `${groceryItem.item} x ${groceryItem.quantity} <br/>`;
});
}
}