Shopping List
Welcome to the last lesson of the JavaScript course! In it, you'll put together everything you've learned and build a small app: a shopping list. You type what you want to buy and its price, click a button, and the item appears in the list. The app adds up the prices, checks what you've typed and lets you cross off what you've already bought. Later, it gets its starting items from a file and remembers your list, so that the list is still there when you open the page again.
You'll use variables, conditions, arrays, loops, functions and objects, and what you've learned about web pages, events and data from a server. Each exercise continues where the previous one ended (the editor starts with its finished code) and adds one piece. At the end, you'll have a whole app that you built step by step.
The page. The page in index.html is ready, so you'll work almost only in script.js. Your script will work with these elements (listed by their id):
list: the list of items (ul), empty for now,total: how much the shopping costs. It's aspan, an element for a piece of text inside a paragraph,nameandprice: the text fields for what to buy and how much it costs (the price field hastype="number": it is meant for numbers),add: the button that adds the item to the list,message: a paragraph for messages, for example when you forget to enter the price.
The style element at the top of the page also has a style for the class bought, which crosses out an item. You'll need this class later. At the top of script.js, we've already found the elements with document.querySelector() and stored them in constants, so you can start right away.
The plan. The shopping list is kept in an array of objects, one object per item, for example { name: "Milk", price: 2 }. This array holds the app's data. The page only shows the data: a function showItems() clears the list on the page and creates an li element for each item in the array. Whenever the data changes, for example when you add an item, the script changes the array and calls showItems() again. It's the same idea as the counter in the lesson on web pages: the value lives in a variable, and the page shows it.
A reminder of how to create an element and put it on the page:
const li = document.createElement("li");
li.textContent = "Milk";
list.append(li);Instructions
- 1
Under the comment
// The shopping list, create a constantitems: an array of three objects with the propertiesname(text) andprice(a number). Put milk for2, bread for3and apples for5in it, each object on its own line. The first one is{ name: "Milk", price: 2 }. Useconstbecause the variable will always hold this same array, even when we add items to it. - 2
Write a function
showItems(). It first clears the list withlist.textContent = "", so that the items don't appear twice when the function is called again. Then, in afor...ofloop overitems, it creates anlifor each item with text likeMilk: €2(a template literal, the text in backticks with${...}, is handy here) and appends it tolist. At the end of the script, callshowItems(), and the three items appear on the page.