-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCC-js-tutorial-part-05.js
308 lines (264 loc) · 7.12 KB
/
FCC-js-tutorial-part-05.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
TODO : Basic Data Structures
*/
// !=> 5.1=>Use an Array to Store a Collection of Data
let yourArray = ['one', 7, 'three', true, false, undefined, null]; // Change this line
// !=> 5.2 => Access an Array's Contents Using Bracket Notation
let myArray = ["a", "b", "c", "d"];
myArray[1] = "e"//not b anymore;
console.log(myArray);
// !=> 5.3 => Add Items to an Array with push() and unshift()
function mixedNumbers(arr) {
// Only change code below this line
arr.unshift('I', 2, 'three');
arr.push(7, 'VIII', 9)
// Only change code above this line
return arr;
}
console.log(mixedNumbers(['IV', 5, 'six']));
// !=> 5.4 => Remove Items from an Array with pop() and shift()
function popShift(arr) {
let popped = arr.pop(); // Change this line
let shifted = arr.shift(); // Change this line
return [shifted, popped];
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));
// !=> 5.5 => Remove Items Using splice()
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
arr.splice(1, 4)
// Only change code above this line
console.log(arr);
// !=> 5.6 => Add Items Using splice()
function htmlColorNames(arr) {
// Only change code below this line
const starIndex = 0
const amountToDelete = 2
arr.splice(starIndex, amountToDelete, 'DarkSalmon', 'BlanchedAlmond')
// Only change code above this line
return arr;
}
console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurquoise', 'FireBrick']));
// !=> 5.7 => Copy Array Items Using slice()
function forecast(arr) {
// Only change code below this line
return arr.slice(2, 4);
}
// Only change code above this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
// !=> 5.8 => Copy an Array with the Spread Operator
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// Only change code below this line
newArr.push([...arr])
// Only change code above this line
num--;
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));
// !=> 5.9 => Combine Arrays with the Spread Operator
function spreadOut() {
let fragment = ['to', 'code'];
let sentence = ['learning', ...fragment, 'is', 'fun']; // Change this line
return sentence;
}
console.log(spreadOut());
// !=> 5.10 => Check For The Presence of an Element With indexOf()
function quickCheck(arr, elem) {
// Only change code below this line
let idx = arr.indexOf(elem)
if (idx >= 0) {
return true
}
return false
// Only change code above this line
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
// !=> 5.11 => Iterate Through All an Array's Items Using For Loops
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) == -1) {
newArr.push(arr[i])
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
// !=> 5.12 => Create complex multi-dimensional arrays
let myNestedArray = [
// Only change code below this line
["unshift", false, 1, 2, 3, "complex", "nested"],
["loop", "shift", 6, 7, 1000, "method"],
["concat", false, true, "spread", "array", ["deep"]],
["mutate", 1327.98, "splice", "slice", "push", [["deeper"]]],
["iterate", 1.3849, 7, "8.4876", "arbitrary", "depth", [[["deepest"]]]]
// Only change code above this line
];
// !=> 5.13 => Add Key-Value Pairs to JavaScript Objects
let foods = {
apples: 25,
oranges: 32,
plums: 28
};
// Only change code below this line
foods['bananas'] = 13
foods['grapes'] = 35
foods['strawberries'] = 27
// Only change code above this line
console.log(foods);
// !=> 5.14 => Modify an Object Nested Within an Object
let userActivity = {
id: 23894201352,
date: 'January 1, 2017',
data: {
totalUsers: 51,
online: 42
}
};
// Only change code below this line
userActivity.data.online = 45
// Only change code above this line
console.log(userActivity);
// !=> 5.15 => Access Property Names with Bracket Notation
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
function checkInventory(scannedItem) {
// Only change code below this line
return foods[scannedItem]
// Only change code above this line
}
console.log(checkInventory("apples"));
// !=> 5.16 => Use the delete Keyword to Remove Object Properties
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
// Only change code below this line
delete foods.oranges
delete foods.plums
delete foods.strawberries
// Only change code above this line
console.log(foods);
// !=> 5.17 => Check if an Object has a Property
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(userObj) {
// Only change code below this line
if (
userObj.hasOwnProperty('Alan') &&
userObj.hasOwnProperty('Jeff') &&
userObj.hasOwnProperty('Sarah') &&
userObj.hasOwnProperty('Ryan')
) {
return true
} return false
// Only change code above this line
}
console.log(isEveryoneHere(users));
// !=> 5.18 => Iterate Through the Keys of an Object with a for...in Statement
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(usersObj) {
// Only change code below this line
let btn = 0
for (let user in usersObj) {
if (usersObj[user].online === true) {
btn += 1
}
}
return btn
// Only change code above this line
}
console.log(countOnline(users));
// !=> 5.19 => Generate an Array of All Object Keys with Object.keys()
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function getArrayOfUsers(obj) {
// Only change code below this line
return Object.keys(obj)
// Only change code above this line
}
console.log(getArrayOfUsers(users));
// !=> 5.20 => Modify an Array Stored in an Object
let user = {
name: 'Kenneth',
age: 28,
data: {
username: 'kennethCodesAllDay',
joinDate: 'March 26, 2016',
organization: 'freeCodeCamp',
friends: [
'Sam',
'Kira',
'Tomo'
],
location: {
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
}
};
function addFriend(userObj, friend) {
// Only change code below this line
userObj.data.friends.push(friend)
return userObj.data.friends
// Only change code above this line
}
console.log(addFriend(user, 'Pete'));