Menu
Coddy logo textTech

הצגת הרשימה

חלק מהיחידה יסודות במסלול ה-JavaScript של Coddy. שיעור 74 מתוך 77.

challenge icon

אתגר

קל

עכשיו, הוסף פונקציה בשם viewList שאינה מקבלת ארגומנטים ומדפיסה את רשימת המצרכים.

הפונקציה תפיק:

Grocery List:
1. Milk
2. Eggs
3. Butter

עבור רשימה עם הפריטים הבאים ["Milk", "Eggs", "Butter"].

אם הרשימה ריקה, הפק:

The grocery list is empty.

 

 

הוסף את הקוד הבא בסוף הקוד שלך כדי לבדוק אותו (במקום קוד הבדיקה הקודם):

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

נסו בעצמכם

// 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.`);
}

// Function to remove an item from the grocery list
function removeItem(item) {
	const index = groceryList.indexOf(item);
	if (index !== -1) {
		groceryList.splice(index, 1);
		console.log(`${item} removed from the grocery list.`);
	} else {
		console.log(`${item} is not in the grocery list.`);
	}
}

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

כל השיעורים ביחידה יסודות

תרגלו בעצמכם: קומפיילר JavaScript אונליין