forked from DevMountain/javascript-2-afternoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.js
235 lines (186 loc) · 7.02 KB
/
practice.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
//Once you complete a problem, open up Chrome and check the answer in the console.
//Create an object called me. Give it a key of name with the value being your name, and another key of age with the value being your age. Then alert your name using dot notation.
var me = {
name: "Spencer Powell",
age: 31
}
//Code here
alert(me.name);
//NEXT PROBLEM
//Make a 'favoriteThings' object that contains the following keys: band, food, person, book, movie, holiday. Have the values to those keys be your favorite thing in that category.
//Code here
var favoriteThings = {
band: "Book on Tapeworm",
food: "Italian",
person: "Haley",
book: "Book of Mormon",
movie: "Star Wars",
holiday: "Christmas"
}
//After you've made your object, add another key named 'car' with the value being your favorite car and then another key named 'brand' with the value being your favorite brand.
//Code here
favoriteThings.car = "Dodge Viper";
favoriteThings.brand = "Christian";
//Now change the food key in your favoriteThings object to be 'Lettuce' and change the book key in your favoriteThings object to be '50 Shades of Gray'.
//Code here
favoriteThings.food = "Lettuce";
favoriteThings.book = "50 Shades of Gray";
//NEXT PROBLEM
//Create an empty Object called backPack. Now, create a variable called 'item'
//and set it equal to the string 'firstPocket'. Using bracket notation,
//add a 'firstPocket' key (or property) to backPack, using 'item'.
//Set the value of that key to 'chapstick'.
//Using dot notation, add another key (or property) to your backPack object
//that is named color, with the value being the color of your backpack.
//Code here
var backPack = {};
var item = 'firstPocket';
backPack[item] = "chapstick";
//After you do the above, alert your entire backPack object.
//Code here
alert(backPack);
//You probably noticed that it just alerted [object Object].
//Alerting to see the data in your Object doesn't work so well.
//Instead, console.log your whole backPack object and then check out the console.
//Code here
console.log(backPack);
//NEXT PROBLEM
//Create an 'alsoMe' object with the following properties name, age, height, gender, married, eyeColor, hairColor. Fill those properties in with the appropriate values.
//Code Here
var alsoMe = {
name: "Spencer Powell",
age: 31,
height: "5'9''",
gender: "Male",
married: "Married",
eyeColor: "Hazel",
hairColor: "Brown"
}
//Now, loop through your object and alert every value. *Tyler --> 24 --> 6'0 --> Male, etc etc
//Code Here
for(var i in alsoMe) {
alert(i + ": " + alsoMe[i]);
}
//NEXT PROBLEM
//Create an Object called 'album' with 5 keys named different song titles that you make up, with the values being the length of each song.
//Code Here
var album = {
a: 3.41,
b: 2.46,
c: 3.15,
d: 4.12,
e: 3.45
}
//Now, loop through your album object alerting every song title individually.
//Code Here
for(i in album) {
alert(i);
}
//NEXT PROBLEM
//Create an object called states that has 5 US states as properties with the values being their population (doesn't have to be accurate).
//Code Here
var states = {
california: 3000000,
texas: 2000000,
utah: 900000,
arizona: 30000,
idaho: 10000
}
//Now, loop through your states object and if the states population is greater than 30K, alert that state.
//Code Here
for(i in states)
if(states[i] > 30000)
alert(i);
//NEXT PROBLEM
var user1 = {
name: 'Tyler McGinnis',
email: null,
pwHash: 'U+Ldlngx2BYQk',
birthday: undefined,
username: 'tylermcginnis33',
age: 0
}
/*Above you're given a user object. Loop through the user object checking to make sure
that each value is truthy. If it's not truthy, remove it from the object. */
//Code Here
for(i in user1) {
if(!user1[i]) {
delete user1[i];
}
}
// console.log(Object.keys(user1).length);
//Once you get your truthy Object, Change the remaining values in the object to be specific to you (name: 'your name', username: 'your username'), rather than my information.
//Code Here
user1 = {name: "Spencer Powell", pwHash: "U+fnoFerHs", userName: "SuperFail" };
//NEXT PROBLEM
var user2 = {
name: 'Tyler McGinnis',
age: 24,
pwHash: 'U+Ldlngx2BYQk',
email: '[email protected]',
birthday: '05/02/1990',
username: 'tylermcginnis33',
sayName: function(){
alert('Email is : ' + this.email);
}
};
//Let's say I, the user, decided to change my name and email address to the following
// name -> 'Tyler S. McGinnis', email -> '[email protected]'. Make that change.
//Code Here
user2.name = 'Tyler S. McGinnis';
user2.email = '[email protected]';
//Now call the sayName method that's on the user object which will alert the users email
//Code Here
user2.sayName();
//NEXT PROBLEM
//Create an empty object called methodCollection.
//Code Here
var methodCollection = {};
/*Now add two methods (functions that are properties on objects) to your methodCollection
object. One called 'alertHello' which alerts 'hello' and another method called logHello
which logs 'hello' to the console. */
//Code Here
methodCollection.alertHello = function(){alert("hello");};
methodCollection.logHello = function(){console.log("hello");};
//Now call your alertHello and logHello methods.
//Code Here
methodCollection.alertHello();
methodCollection.logHello();
//NEXT PROBLEM
// Create a function called MakePerson which takes in name, birthday, ssn as its parameters and returns a new object with all of the information that you passed in.
//Code Here
var MakePerson = function(name, birthday, ssn) {
return {
name: name,
birthday: birthday,
ssn: ssn
};
};
//NEXT PROBLEM
// Create a function called MakeCard which takes in cardNumber, expirationDate, and securityCode to make a Credit Card object and returns that object so that whenever you invoke MakeCard, you get a brand new credit card.
//Code Here
var MakeCard = function(cardNumber, expirationDate, securityCode) {
return {
cardNumber: cardNumber,
expirationDate: expirationDate,
securityCode: securityCode
};
};
//NEXT PROBLEM
/* As of this point you should have a MakePerson and a MakeCard function which returns you either a person or a credit card object.
Now, create a bindCard function that takes in a person object as its first parameter and a creditcard object as its second parameter.
Have bindCard merge the two parameters together into a new object which contains all the properties from the person as well as the creditcard. While Object.assign would give you the answer, specRunner requires an answer without using it.
*/
//Code Here
var bindCard = function(person, card) {
// console.log(person.name);
// console.log(person.birthday);
var result = {};
for(var i in person) {
result[i] = person[i];
}
for(var j in card) {
result[j] = card[j];
}
return result;
};