Lab Task7
Lab Task7
Instructions:
Objective: Build a simple shopping list application using JavaScript that allows users to manage
their shopping items.
Instructions:
Instructions:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Shopping List</title>
</head>
<body>
<h1>Shopping List</h1>
<script src="script.js"></script>
</body>
</html>
var shoppingList = [ ];
4. Step 2: Create the addItem Function
o Define a function named addItem that takes one parameter (the item to be added).
o Inside the function, push the item into the shoppingList array.
o Log a message confirming the addition of the item.
o
5. Step 3: Create the removeItem Function
o Define a function named removeItem that takes an index as a parameter.
o Inside the function, check if the index is valid (between 0 and the length of the
shoppingList array).
o If valid, remove the item using splice and log a confirmation message. If invalid,
log an error message.
6. Step 4: Create the displayItems Function
o Define a function named displayItems that does not take any parameters.
o Use a loop to iterate through the shoppingList array and log each item along with
its index.
7. Step 5: Create the clearList Function
o Define a function named clearList that resets the shoppingList array to an empty
array.
o Log a message confirming that the list has been cleared.
8. Step 6: Test Your App
o Add some items to your shopping list using the addItem function.
o Display the current items in your shopping list using the displayItems function.
o Remove an item using the removeItem function and display the list again.
o Clear the shopping list using the clearList function and confirm that the list is
empty.
9. Example Usage:
o Include the following example calls in your shoppingList.js file to test your
functions:
javascript
Copy code
addItem("Apples");
addItem("Bread");
addItem("Milk");
displayItems();
removeItem(1); // Remove Bread
displayItems();
clearList();