Lab 2
Array Manipulation
Scenario
You have been assigned to work on a feature that handles large sets of data in the form of arrays. You need to build efficient functionality to add, remove, and filter data from arrays based on user interactions. In this assignment, you will practice manipulating arrays with JavaScript methods and writing code that dynamically handles user inputs.
Learning Objectives
By the end of this activity, you will have demonstrated your ability to:
- Use array methods such as
push()
,pop()
,shift()
, andunshift()
to manipulate arrays. - Filter and transform arrays using JavaScript array methods.
- Dynamically display the contents of arrays on a webpage.
- Write clean, maintainable JavaScript code to handle array operations.
Instructions
Task 1: Array Manipulation Basics
- Create an empty array called
shoppingList
. - Write a function called
addItem
that takes an item as a parameter and adds it to theshoppingList
array. - Write a function called
removeLastItem
that removes the last item from theshoppingList
array. - Write a function called
displayList
that logs all items in theshoppingList
array to the console.
Task 2: Filter and Search an Array
- Modify the
addItem
function to only add the item if it is not already in theshoppingList
array. - Write a function called
filterItems
that takes a search term as a parameter and returns all items in theshoppingList
that contain that search term (case-insensitive).
Task 3: Render the List in the Browser
- Create an HTML page with an input field, an “Add Item” button, and an unordered list to display the items.
- Write a JavaScript function that adds items to the array and updates the displayed list dynamically when the button is clicked.
- Write another function that removes the last item and updates the displayed list when a “Remove Last Item” button is clicked.
Submission Instructions
- Complete all of the tasks outlined above.
- Test your code in a browser and ensure that it works as expected.
- Submit your code by sharing the link to a GitHub repository containing the files via Canvas.
Reflection Questions
After completing this lab, reflect on the following questions:
- How did array methods like
push()
andfilter()
help you manipulate data in this activity? - What challenges did you face when trying to filter and search for items in the array?
- How would you modify this code to make it more efficient or user-friendly?