- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicDataStructures.js
More file actions
Latest commit
279 lines (233 loc) · 7.02 KB
/
Copy pathbasicDataStructures.js
File metadata and controls
279 lines (233 loc) · 7.02 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
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
constlog=console.log;
functionzeroArray(m,n){
// Creates a 2-D array with m rows and n columns of zeroes
letnewArray=[];
for(leti=0;i<m;i++){
// Adds the m-th row into newArray
letrow=[];/* <----- row has been declared inside the outer loop.
Now a new row will be initialised during each iteration of the outer loop allowing
for the desired matrix. */
for(letj=0;j<n;j++){
// Pushes n zeroes into the current row to create the columns
row.push(0);//if you wanted to add other values change the zero
}
// Pushes the current row, which now has n zeroes in it, to the array
newArray.push(row);
}
returnnewArray;
}
letmatrix=zeroArray(3,2);
console.log(matrix);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//UNSHIFT AND PUSH METHODS (which add to beggining and end of arrays)
letmyArrayChange=["a","b","c","d"];
myArrayChange.unshift("addToBeginningOfArray");//beginning
myArrayChange.push("addToEndOfArray")//end
log(myArrayChange)// [ 'addToBeginningOfArray', 'a', 'b', 'c', 'd', 'addToEndOfArray' ]
//SHIFT AND POP METHOD (which removes from beginning and end of array)
functionpopShift(arr){
letpopped=arr.pop();// change this line
letshifted=arr.shift();// change this line
return[shifted,popped];
}
// do not change code below this line
console.log(popShift(['challenge','is','not','complete']));//challenge complete
letpopShift2=["start"," middle"," end"];
letremoveStart=popShift2.shift();//start
letremoveEnd=popShift2.pop();//end
log(popShift2)//middle
//SPLICE MEHOD////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vararray=[1,2,3,4]
varitem=3
varindex=array.indexOf(item);
if(index!==-1)array.splice(index,1);
console.log(array)
letarraySplice=['today','was','not','so','great'];
arraySplice.splice(2,2);
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']
letarraySplice2=['I','am','feeling','really','happy'];
letnewArraySplice=arraySplice2.splice(3,2);
// newArray equals ['really', 'happy']
functionsumOfTen(arr){
// change code below this line
arr.splice(1,2);
// change code above this line
returnarr.reduce((a,b)=>a+b);
}
console.log(sumOfTen([2,5,1,5,2,1]));//10
//Replacing values with splice
varmonths=['Jan','March','April','June'];
months.splice(1,0,'Feb');
// inserts at 1st index position
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
functionreplaceCartoons(arr){
arr.splice(1,1,"Jerry");
returnarr;
}
log(replaceCartoons(["Morty","Rick","Mr. Noob Noob","Mr. Poopy B-Hole"]));
//[ 'Morty', 'Jerry', 'Mr. Noob Noob', 'Mr. Poopy B-Hole' ]
functioncolorChange(arr,index,newColor){
arr.splice(index,1,newColor);
returnarr;
}
letcolorScheme=['#878787','#a08794','#bb7e8c','#c9b6be','#d1becf'];
colorScheme=colorChange(colorScheme,2,'#332327');
// we have removed '#bb7e8c' and added '#332327' in its place
// colorScheme now equals ['#878787', '#a08794', '#332327', '#c9b6be', '#d1becf']
functionhtmlColorNames(arr){
arr.splice(0,2,"DarkSalmon","BlanchedAlmond");
returnarr;
}
console.log(htmlColorNames(['DarkGoldenRod','WhiteSmoke','LavenderBlush','PaleTurqoise','FireBrick']));
// SLICE METHOD EXTRACTS FROM ARRAY ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// first parameter starts index second ends with , array stays intact.
letsliceWord=["beginning","unwanted","notNeeded","middle","end"];//Array stays intact
letextractWords=sliceWord.slice(1,3);
log(extractWords)//[ 'unwanted', 'notNeeded' ]
functionforecast(arr){
arr.slice(2,4);// Not modefied, only extracts data
arr=arr.slice(2,4)// Array is modefied
returnarr;
}
console.log(forecast(['cold','rainy','warm','sunny','cool','thunderstorms']));
//
functioncopyMachine(arr,num){
letnewArr=[];
while(num>=1){
// change code below this line
newArr.push([...arr]);
// change code above this line
num--;
}
returnnewArr;
}
// change code here to test different cases:
console.log(copyMachine([true,false,true],2));
//
functionspreadOut(){
letfragment=['to','code'];
letsentence=['learning',...fragment,'is','fun'];// change this line
returnsentence;
}
console.log(spreadOut());
//Check For The Presence of an Element With indexOf()///////////////////////////////////////////////////////////////////////////////////function
functionquickCheck(arr,elem){
if(arr.indexOf(elem)>0||arr.indexOf(elem)!==-1){
returntrue;
}else{returnfalse;}
}
// change code here to test different cases:
console.log(quickCheck(['squash','onions','shallots'],'mushrooms'));
//
functionfilteredArray(arr,elem){
letnewArr=[];
// change code below this line
for(leti=0;i<arr.length;i++){
if(arr[i].indexOf(elem)<0){
newArr.push(arr[i]);
}
}
// change code above this line
returnnewArr;
}
// change code here to test different cases:
console.log(filteredArray([[3,2,3],[1,6,3],[3,13,26],[19,3,9]],3));
//
letusers={
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
functionisEveryoneHere(obj){
// change code below this line
// if(users.hasOwnProperty("Alan","Jeff","Sarah","Ryan")){
// return true;
// }else{return false};
if("Alan","Jeff","Sarah","Ryan"inusers){
returntrue;
}else{returnfalse;}
}
console.log(isEveryoneHere(users));
//
letusers={
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
functioncountOnline(obj){
letcount=[];
for(letiinobj){
if(obj[i].online==true){
count++;
}}
returncount;
}
console.log(countOnline(users));
//Access Property Names with Bracket Notation
letfoods={
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
console.log(Object.keys(foods));
functioncheckInventory(scannedItem){
returnfoods[scannedItem];
}
console.log(checkInventory("bananas"));
//Modify an Array Stored in an Object
letuser={
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'
}}};
functionaddFriend(userObj,friend){
for(letiinuserObj){
if(userObj[i].hasOwnProperty("friends")){
userObj[i].friends.push(friend);
}
}
returnuserObj.data.friends;
}
console.log(addFriend(user,'Pete'));