forked from SunJieMing/js-minicamp-homework-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises.js
157 lines (137 loc) · 4.51 KB
/
exercises.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
'use strict';
//Do not change any of the function names
function makeCat(name, age) {
//create a new object with a name property with the value set to the name argument
//add an age property to the object with the value set to the age argument
//add a method called meow that returns the string 'Meow!'
//return the object
var cat = {
name: name,
age: age,
meow: function() {
return 'Meow!';
} // end meow method
}; // end object cat
return cat;
}
function addProperty(object, property) {
//add the property to the object with a value of null
//return the object
//note: the property name is NOT 'property'. The name is the value of the argument called property (a string)
object[property] = null;
return object;
}
function invokeMethod(object, method) {
//method is a string that contains the name of a method on the object
//invoke this method
//nothing needs to be returned
object[method]();
}
function multiplyMysteryNumberByFive(mysteryNumberObject) {
//mysteryNumberObject has a property called mysteryNumber
//multiply the mysteryNumber property by 5 and return the product
return mysteryNumberObject.mysteryNumber * 5;
}
function deleteProperty(object, property) {
//remove the property from the object
//return the object
delete object[property];
return object;
}
function newUser(name, email, password) {
//create a new object with properties matching the arguments passed in.
//return the new object
var obj = {
name: name,
email: email,
password: password
};
return obj;
}
function hasEmail(user) {
//return true if the user has a value for the property 'email'
//otherwise return false
if (user.email) return true;
else return false;
}
function hasProperty(object, property) {
//return true if the object has the value of the property argument
//property is a string
//otherwise return false
if (object.hasOwnProperty(property)) return true;
else return false;
}
function verifyPassword(user, password) {
//check to see if the provided password matches the password property on the user object
//return true if they match
//otherwise return false
if (user.password === password) return true;
else return false;
}
function updatePassword(user, newPassword) {
//replace the existing password on the user object with the value of newPassword
//return the object
user.password = newPassword;
return user;
}
function addFriend(user, newFriend) {
//user has a property called friends that is an array
//add newFriend to the end of the friends array
//return the user object
user.friends.push(newFriend);
return user;
}
function setUsersToPremium(users) {
//users is an array of user objects.
//each user object has the property 'isPremium'
//set each user's isPremium property to true
//return the users array
for (var i in users) {
users[i].isPremium = true;
} // end for
return users;
}
function sumUserPostLikes(user) {
//user has an array property called 'posts'
//posts is an array of post objects
//each post object has an integer property called 'likes'
//sum together the likes from all the post objects
//return the sum
var sum = 0;
for (var i in user.posts) {
sum += user.posts[i].likes;
} // end for
return sum;
}
function addCalculateDiscountPriceMethod(storeItem) {
//add a method to the storeItem object called 'calculateDiscountPrice'
//this method should multiply the storeItem's 'price' and 'discountPercentage' to get the discount
//the method then subtracts the discount from the price and returns the discounted price
//example:
//price -> 20
//discountPercentage -> .2
//discountPrice = 20 - (20 * .2)
// return storeItem
storeItem.calculateDiscountPrice = function() {
return storeItem.price - (storeItem.price * storeItem.discountPercentage);
};
return storeItem;
}
//Do not modify code below this line.
////--------------------------------
module.exports = {
makeCat: makeCat,
addProperty: addProperty,
invokeMethod: invokeMethod,
multiplyMysteryNumberByFive: multiplyMysteryNumberByFive,
deleteProperty: deleteProperty,
newUser: newUser,
hasEmail: hasEmail,
hasProperty: hasProperty,
verifyPassword: verifyPassword,
updatePassword: updatePassword,
addFriend: addFriend,
setUsersToPremium: setUsersToPremium,
sumUserPostLikes: sumUserPostLikes,
addCalculateDiscountPriceMethod: addCalculateDiscountPriceMethod
};