-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathpractice.js
More file actions
263 lines (168 loc) · 5.99 KB
/
practice.js
File metadata and controls
263 lines (168 loc) · 5.99 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
////////// PROBLEM 1 //////////
/*
Add a getUserName property on the user object below
This property should be a function that returns the username on the object ( bencallis )
You must use the this keyword
*/
let user = {
username: "bencallis",
email: "bencallis1@gmail.com",
// Code here
};
////////// PROBLEM 2 //////////
/*
Modify the getUsername function to return the username property of an object
You must use the this keyword
*/
let user1 = {
username: "iliketurtles",
age: 13,
email: "iliketurtles@gmail.com",
};
const getUsername = function () {
// Code here
};
const username = getUsername.call(user1);
////////// PROBLEM 3 //////////
/*
Create an otherUsername variable to call getOtherUsername with user2 to get its username property
You must use the call method
*/
let user2 = {
username: "BillyBob",
age: 19,
email: "billybob21@gmail.com",
};
function getOtherUsername() {
return this.username;
};
// Code here
////////// PROBLEM 4 //////////
/*
Using the user1 and user2 objects and the getOtherUsername function defined above,
create two variables username1 and username2 that call getOtherUsername to get the respective user usernames
You must use the call method
username1 results in a value of "iliketurtles"
username2 results in a value of "BillyBob"
*/
// Code here
////////// PROBLEM 5 //////////
/*
Create a variable called result that uses the apply method to call add and return the value 11
You must use the apply method
*/
const obj = {
num: 5,
};
const nums = [1, 2, 3];
function add(a, b, c) {
return this.num + a + b + c;
}
// Code here
////////// PROBLEM 6 //////////
/*
Using the favRapper and getName function below,
create a variable called name that uses the bind method to call getName and return the name from the favRapper object
You must use the bind method
*/
const favRapper = {
name: "Xzibit",
birthYear: 1974,
};
const getName = function () {
return this.name;
};
// Code here
////////// PROBLEM 7 //////////
/*
Here we have a constructor function named Car.
Finish the code so that this constructor function will create an object using the given parameters.
Make sure the property names match the parameter names
*/
function Car (color, make, year){
// Code here
}
////////// PROBLEM 8 //////////
/*
Add another property called moveCar to the CarMaker constructor function
This function should increment the value of move by 10 every time it is called
You must use the this keyword
*/
function CarMaker (make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.move = 0;
// Code here
};
////////// PROBLEM 9 //////////
/*
Create a constructor function called Player
Player should take in four parameters: name, age, team, and pointsScored
The function should use these four parameters to return a new object
You should also create a method named addPoints inside of this constructor function
This method should take in a number parameter
This number parameter should be added to the player's pointsScored value and should then return that same updated pointsScored value
*/
// Code here
////////// PROBLEM 10 //////////
/*
Here we have a constructor function named Restaurant that has a method inside the function
Remove the existing method addStars and use it to create a prototype method for this constructor function
Make sure to use the same naming convention for the prototype method
*/
function Restaurant(name, type, stars) {
this.name = name;
this.type = type;
this.stars = stars;
this.addStars = function (num) {
this.stars += num;
return this.stars;
};
}
// Code here
////////// PROBLEM 11 //////////
/*
Here we have a constructor function named Person
This function takes in 5 parameters, the last being an array of strings representing the person's friends
Create a prototype method named addFriend that will take in a string parameter (new friend)
This function will add that new friend to the person's friends array
Also, create a second prototype method named removeFriend that will take in a string parameter (person to unfriend)
find that friend in the user's friends array (don't worry about capitalization) and remove it from the array
*/
function Person(name, age, hometown, email, friends) {
this.name = name;
this.age = age;
this.hometown = hometown;
this.email = email;
this.friends = friends;
}
// Code here
////////// PROBLEM 12 //////////
/*
Write a constructor function called User
This function should take in 4 parameters: name, age, email, and savedPosts
Name and email will be strings, age will be a number and savedPosts will be an array of objects
These objects will each have 3 properties: id (a number), title (a string), and rating (a number between 1 and 5)
These objects are the posts that the user will have saved to their account
Once the User constructor function is created, write a prototype method for the User function
Name this method addSavedPost
It should take in three parameters: id (a number), title (a string) and rating (a number)
Use these parameters to create a new object and add it to the savedPosts array
Make sure to name the properties the same as described previously (id, title, rating)
*/
// Code here
////////// PROBLEM 13 //////////
/*
Using the User constructor function built as part of Problem 11, write a prototype method named removeSavedPost
This function will take in a number parameter representing the post id
Use this id to find and remove the matching object from the savedPosts array
*/
// Code here
////////// PROBLEM 14 //////////
/*
Using the User constructor function built as part of Problem 11 & 12, write a prototype method named changePostRating
This function will take in 2 number parameters: id (a number) and newRating (a number)
Use this id to find the matching object in the savedPosts array and update its rating with the newRating parameter
*/
// Code here