-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·47 lines (40 loc) · 1.26 KB
/
script.js
File metadata and controls
executable file
·47 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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
* - 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.
*/
// - Build an array with 8 items
let items = [
"apple",
"banana",
"cherry",
"date",
"elderberry",
"fig",
"grape",
"honeydew",
];
console.log("Original Array:", items);
// - Remove the last item
let lastItem = items.pop();
console.log("After removing the last item:", items);
// - Add the last item as the first item on the array
items.unshift(lastItem);
console.log("After adding the last item as the first item:", items);
// - Sort the items by alphabetical order
items.sort();
console.log("After sorting alphabetically:", items);
// - Use the find() method to find a specific item in the array
let itemToFind = "date";
let foundItem = items.find((item) => item === itemToFind);
console.log(`Found item: ${foundItem}`);
// - Remove the item you found using the find method from the array
let index = items.indexOf(foundItem);
if (index !== -1) {
items.splice(index, 1);
}
console.log("After removing the found item:", items);