Mini Product Store
A JavaScript activity where students build a simple product store using arrays, loops, functions, DOM manipulation, and event listeners.
#teaching #activity #javascript #dom #arrays #loops
Build a simple product store entirely with JavaScript. The goal is not to create a beautiful website, but to practice thinking in data instead of copy-pasting HTML.
Learning Objectives
By the end of this activity, students should be able to:
- Store products in an array of objects.
- Create reusable functions that work for any product.
- Generate HTML using a
forloop. - Display cart items using
forEach(). - Update the page using DOM manipulation.
- Respond to user actions with event listeners.
- Use template literals and
ifstatements to build dynamic interfaces.
The Big Idea
Project Files
| File | Purpose |
|---|---|
index.html | Minimal HTML structure with empty containers. |
style.css | Basic styling. No changes required. |
script.js | Your working file containing the TODOs. |
SOLUTION.js | Reference solution. Check only after attempting the activity yourself. |
Activity Flow
Open the Project
Open index.html in your browser and script.js in your code editor.
Complete the TODOs
Finish each task in order.
- Build
createCard(product)using a template literal and attach an event listener. - Build
renderProducts()using aforloop. - Build
addToCart(product)by adding products into the cart array. - Build
renderCart()using anifstatement andforEach().
Save your work and refresh the browser after each step.
Test Your Application
Verify that:
- Products appear automatically from the JavaScript array.
- Clicking Add to cart updates the cart.
- The total updates correctly.
- An empty cart displays an appropriate message.
Requirements
Students must demonstrate all of the following:
letandconst- An array of product objects
- At least three reusable functions
- At least one
forloop - At least one
forEach() - DOM manipulation
- At least one
addEventListener - At least one template literal
- At least one
ifstatement - No hardcoded product HTML
- No per-product functions such as
addApple()oraddBanana()
Expected Output
When completed, the application should:
- Display one product card for every object inside the
productsarray. - Allow users to add products to the cart.
- Automatically update the total price.
- Display "Your cart is empty." before any products are added.
- Correctly add duplicate products if the same button is clicked multiple times.
F12) while working. Read every error
message carefully before asking for help. Most bugs tell you exactly where they
are hiding. Computers are surprisingly honest compared to humans.Stretch Goals
Students who finish early may:
- Add a
quantityproperty to each product. - Display a Free Shipping message when the total exceeds ₱100.
- Add a Clear Cart button with its own event listener.
Submission
- Add your full name and section at the top of
script.js. - Submit the required files according to your instructor's instructions.
- Be prepared to explain every line of your code.
Grading Rubric
| Category | Points |
|---|---|
| JavaScript Logic | 25 |
| Reusable Functions | 20 |
Loops (for and forEach) | 15 |
| DOM Manipulation | 15 |
| Code Organization | 10 |
| Readability | 10 |
| Following Instructions | 5 |
| Total | 100 |
Resources
The starter project for this activity is available on GitHub.
- Activity Template:
https://github.com/jpynzon/js-product-store-activity
Clone or download the repository before starting the activity. The project already contains the HTML structure, CSS styling, and JavaScript TODOs. Your task is to complete the missing JavaScript logic inside script.js.
Dynamic Portfolio Website
Build a personal portfolio website using JavaScript arrays, loops, and DOM manipulation instead of hardcoded HTML.
Exercises
Practice problems that build skill through repetition.