-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pull request #77
base: main
Are you sure you want to change the base?
pull request #77
Conversation
update |
@@ -2,10 +2,46 @@ | |||
* Challenge: Build and modify an array | |||
* - Build an array with 8 items | |||
* - Remove the last item | |||
* - Add the last item as the first item on the array | |||
* - Add the last item as the first item on the array | |||
* - Sort the items by alphabetical order | |||
* - 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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.