Congratulations!
Congratulations, your shopping list works, and you've reached the end of the JavaScript course! The app loads its starting items from a file, adds new ones, checks what you type, adds up the prices, lets you cross off and remove what you've bought, and remembers the list for next time. And you wrote all of it yourself.
One last improvement. Cross off the milk, and the amount to pay stays the same. In the shop, it's more useful to see how much the items you haven't bought yet will cost. So in getTotal(), add only the prices of the items whose bought is false. The condition !item.bought (not bought) is just what you need. And since showItems() runs after every click, the amount updates by itself.
What you've learned. Look at how every part of the course found its place in your app:
- Variables and constants hold the elements of the page, the name and the price.
- Conditions check the input and decide whether an item is crossed out.
- An array holds the shopping list, and
push()adds to it. - Loops go through the items to show them and to add up their prices.
- Functions split the app into parts with clear jobs:
showItems()shows,getTotal()computes,saveItems()saves andloadItems()loads. - Objects keep together what belongs to one item: its name, its price and whether it's bought.
- Array methods:
filter()keeps only the items that aren't bought, in one line. - The page and its events: you find elements, change their text and classes, create new ones, react to clicks and show a message when something's wrong.
- Waiting: with
asyncandawait,loadItems()waits for the file while the page keeps working. - Data:
fetch()loads the starting items from a JSON file, andlocalStoragekeeps the list when the page is closed.
Save your app. The app consists of three files, and they must be in the same folder:
- Create a new folder on your computer, for example
shopping-list. - Open a plain text editor, such as Notepad on Windows or TextEdit on a Mac (switched to plain text in the Format menu), or a code editor such as Visual Studio Code. Paste the code from
index.htmland save it in the folder asindex.html. - In the same way, save
script.jsanditems.jsonin the same folder, each under its own name.
If you now double-click index.html, the app opens, but it keeps showing Loading…. The browser opens the page straight from the disk, at an address that starts with file://, and it doesn't let such a page load other files with fetch(). The console shows why: in Chrome, an error about the CORS policy, and TypeError: Failed to fetch. Your message that the list couldn't be loaded doesn't appear either, because fetch() doesn't get any answer, not even a 404. It fails with an error, which ends loadItems() at the await.
The page has to come from a web server, as pages on the internet do. A simple way to get one is Visual Studio Code with the Live Server extension: open your folder in VS Code (File, Open Folder), install Live Server in the Extensions panel on the left, and click Go Live at the bottom right. The app opens in your browser at an address like http://127.0.0.1:5500/index.html, and the starting items load. Live Server runs only on your computer and only while VS Code is open, so nobody else can open the address. It's for trying out your pages.
In the browser, the console is hidden at first: open the developer tools with F12 (on a Mac Cmd+Option+I) and switch to the Console tab. There you'll find what your script prints, and its errors. And when you reload the page, your list is still there, because the browser keeps it in localStorage. Each address has its own: a page opened from a file can save in localStorage too, but what it saves isn't there at http://127.0.0.1:5500, and the other way around.
What's next. You now know the basics that every JavaScript program is built from, in the browser too. Here are some ideas for what to do next:
- Keep improving your list. You already know everything these need: a Remove button for each item (one handler for the whole list helps), adding an item by pressing Enter, a quantity for each item, sorting by price, or a message for when
fetch()itself fails (tryandcatch). You learn the most by solving your own problems. - TypeScript is JavaScript with types: you write down what kind of value each variable and parameter holds, and TypeScript finds many mistakes before the program even runs. Bigger apps are usually written in it, and the TypeScript course follows on from this one.
- React is a library for building web apps out of components. It's built on exactly what you've learned here: data, and a page that shows it. The React course follows on from this one too.
And if you haven't taken them yet, the HTML course and the CSS course will teach you to build the whole page and its look yourself, not just the script.
Instructions
- 1
Change
getTotal()so that it adds up only the prices of the items that aren't bought. With the bread crossed off, the page showsTo pay: €13. Click the items, and the amount changes.