-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-32-Array-Methods-Part-2.js
More file actions
211 lines (116 loc) Β· 4.7 KB
/
Copy pathDay-32-Array-Methods-Part-2.js
File metadata and controls
211 lines (116 loc) Β· 4.7 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Day 32: Array Methods Part 2 - Finding & Extracting Data π
// Method 1: indexOf() - Find the Position
// What it does: finds the INDEX (position) of an item. returns -1 if not found.
let fruits = ["apple", "banana", "orange", "grape", "mango"];
console.log(fruits.indexOf("orange")); // Output: 2 (postion 2)
console.log(fruits.indexOf("banana")); // Output: 1
console.log(fruits.indexOf("kiwi")); // Output: -1 (not found!)
// Why -1? Because -1 is NEVER a valid index, so it means "doesn't exist"
// Real use case:
// check if a user's favorite fruit is in stock
let inStock = ["apple", "banana", "orange"];
let userFavorite = "banana";
if (inStock.indexOf(userFavorite) !== -1) {
console.log(`Great news! We have ${userFavorite} in stock!`)
} else (
console.log(`Sorry, ${userFavorite} is out of stock.`)
)
// Method 2: includes() - Simple Yes/No Check
// What it does: Returns true if item exists, false if it doesn't. MUCH cleaner than indexOf()!
let colors = ["red", "blue", "green", "yellow"];
console.log(colors.includes("blue")); // output: true
console.log(colors.includes("purple")); // output: false
if (colors.includes("red")) {
console.log("Red is in the list!");
}
// Real use case:
// check if user has permission
let userPermissions = ["read", "write", "delete"];
if (userPermissions.includes("delete")) {
console.log("Warning: You can delte files!"); // this runs
} else {
console.log("You cannot delete files.");
}
// Method 3: slice() - Extract a Portion
// what it does: creates a new array from a section of the original. original stays unchanged!
// Syntax: array.slice(startIndex, endIndex)
let numbers = [10, 20, 30, 40, 50, 60, 70];
let portion = numbers.slice(2, 5); // get items from 2 to 5 (not including 5)
console.log(portion);
console.log(numbers);
console.log(numbers.slice(3)); // from index 3 to the end
console.log(numbers.slice(-3)); // last 3 items (netaive index counts from end!)
// Real use case:
// get top 3 players fro leaderboard
let leaderboard = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"];
let topThree = leaderboard.slice(0, 3)
console.log("π Top 3 Players:");
console.log(topThree);
// Challenge 1: Find and Replace position
let queue = ["Hasan", "Fatima", "Ali", "Sara", "Karim"];
let positionOfAli = queue.indexOf("Ali");
let everyoneBeforeAli = queue.slice(0, positionOfAli);
// wtf?? how am i supposed to slice everyone after ali?? π
// i'll do what i know hmph π
let personAfterAli = positionOfAli + 1;
let everyoneAfterAli = queue.slice(personAfterAli, queue.length);
// shit. too many variables π
console.log(everyoneBeforeAli);
console.log(everyoneAfterAli);
// Challenge 2: Menu item checker
let menu = ["burger", "pizza", "pasta", "salad", "fries", "soda"];
let order = ["pasta", "wings", "soda"];
for (let i = 0; i < order.length; i++) {
if (menu.includes(order[i])) {
console.log(`We have ${order[i]}`);
} else {
console.log(`Sorry, no ${order[i]}`);
}
}
// Challenge 3: Top & Bottom scorers
let scores = [92, 87, 95, 78, 85, 90, 73, 88, 91, 76];
let top3 = scores.slice(0, 3);
let bottom3 = scores.slice(-3); // will this work?? ππ
console.log(scores.includes(85));
console.log(scores.indexOf(95));
// Challenge 4: Banned words filter
let bannedWords = ["badword1", "badword2", "spam"];
let userComment = "This is spam and badword1 content";
let words = userComment.split(" ");
for (let i = 0; i < bannedWords.length; i++) {
if (words.includes(bannedWords[i])) {
console.log(`Comment contains banned word: ${bannedWords[i]}`);
} else {
console.log("Comment is clean")
}
}
// Challenge 5: Inventory slice master
let inventory = ["sword", "shield", "potion", "armor", "map", "torch", "rope", "key", "book", "gem"];
let itemsFrom3To5 = inventory.slice(3, 6);
let itemsExpectFirst2 = inventory.slice(2);
let minusLast3 = inventory.length - 3;
let itemsExpectLast3 = inventory.slice(0, minusLast3);
let itemsFromMiddle = inventory.slice(3, 7);
console.log(inventory.includes("key"));
// Mega challenge: Friend list manager
let friends = ["Ahmed", "Nadia", "Rafi", "Sara", "Karim", "Fatima", "Ali"];
if (friends.includes("Rafi")) {
console.log(`Rafi is friend #${friends.indexOf("Rafi")}`);
}
let closeFriends = friends.slice(0, 3);
console.log("Is Zara your friend?")
if (friends.includes("Zara")) {
console.log("yes, this bitch is my friend π");
} else {
console.log("nope, idk this bitch π");
}
let seeFriends2to5 = friends.slice(2, 6);
function checkFriend(name) {
if (friends.includes(name)) {
return true;
} else {
return false;
}
}
console.log(checkFriend("Sara"));
console.log(checkFriend("Bob"));