Menu
Coddy logo textTech

Remove Item

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 73 of 77.

challenge icon

Challenge

Easy

Now, add a function named removeItem that gets a string (the item) and removes it from the list.

In the end, the function will output:

Milk removed from the grocery list.

For the input "Milk".

And if the item does not exist in the list:

Milk is not in the grocery list.

 

 

Add the following code at the end of your code to test it (instead of the previous testing code):

addItem("Milk");
addItem("Bread");
addItem("Eggs");
removeItem("Bread");
removeItem("Cheese");

Try it yourself

// Grocery list array
let groceryList = [];

// Function to add an item to the grocery list
function addItem(item) {
	groceryList.push(item);
	console.log(`${item} added to the grocery list.`);
}

addItem("Milk");
addItem("Bread");
addItem("Eggs");

All lessons in Fundamentals