Conversation
|
update |
| * - Use the find() method to find a specific item in the array | ||
| * - Remove the item you found using the find method from the array. | ||
| */ | ||
|
|
There was a problem hiding this comment.
// Step 1: Build an array with 8 items
let items = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"];
console.log("Initial array:", items);
// Step 2: Remove the last item
let lastItem = items.pop();
console.log("Array after removing last item:", items);
console.log("Last item:", lastItem);
// Step 3: Add the last item as the first item on the array
items.unshift(lastItem);
console.log("Array after adding last item to the start:", items);
// Step 4: Sort the items by alphabetical order
items.sort();
console.log("Array sorted alphabetically:", items);
// Step 5: Use the find() method to find a specific item in the array
let itemToFind = "cherry";
let foundItem = items.find(item => item === itemToFind);
console.log("Found item:", foundItem);
// Step 6: Remove the item you found using the find() method from the array
if (foundItem) {
items = items.filter(item => item !== foundItem);
console.log(Array after removing ${foundItem}:, items);
} else {
console.log(${itemToFind} not found in the array.);
}
No description provided.